所谓切片操作就是在给定的字符串中有规律的选取部分元素组成新串的操作。
采用 str[参数1:参数2:参数3] 的形式进行操作:
1 password=‘0123456789‘ 2 3 print(password[0:5:1]) #01234------基本操作 4 print(password[1:5:2]) #13---------步长操作 5 6 print(password[:5:]) #01234------默认操作 7 8 print(password[-1:-11:-1]) #9876543210--------逆序操作 9 print(password[::-1]) #9876543210--------默认逆序操作 10 11 print(password[-2:-9:-2]) #8642---------逆序步长 12 print(password[-9:-2:1]) #1234567------正序操作 13 14 print(password[0:8:-2]) #没有输出 15 print(password[-2:-9:1]) #没有输出
在python中查找操作一般常用的有:find、index、count、rfind、rindex
find(‘目标子串’,位置1,位置2)
index:
count:
rfind:
rindex:
1 str=‘hello python nice to meet you‘ 2 3 print(str.find(‘nice‘)) #13 4 print(str.find(‘nice‘,10,17)) #13 5 print(str.find(‘hi‘)) #-1 6 7 print(str.index(‘nice‘)) #13 8 print(str.index(‘nice‘,10,17)) #13 9 # print(str.index(‘hi‘)) # erroy 10 11 print(str.count(‘nice‘)) #1 12 print(str.count(‘nice‘,10,17)) #1 13 print(str.count(‘hi‘)) #0 14 15 print(str.rfind(‘nice‘)) #13 16 print(str.rfind(‘nice‘,10,17)) #13 17 print(str.rfind(‘hi‘)) #-1 18 19 print(str.rindex(‘nice‘)) #13 20 print(str.rindex(‘nice‘,10,17)) #13 21 #print(str.rindex(‘hi‘)) # erroy
在python中修改操作一般常用的有:replace、split、join三个重要的方法。另外还有大小写转换、空白格删除、对其等非重要方法
replace(‘旧子串’,‘新子串’,替换次数)
split(‘分割标记子串’,查找的次数)
分割字符串. join(列表)
1 str=‘hello python nice to meet you‘ 2 3 newstr=str.replace(‘to‘,‘kkk‘,1) 4 print(str) # hello python nice to meet you 5 print(newstr) # hello python nice kkk meet you 6 7 list=str.split(‘to‘) 8 print(list) #[‘hello python nice ‘, ‘ meet you‘] 9 10 List=[‘aa‘,‘bb‘,‘cc‘,‘dd‘] 11 NewStr=‘ and ‘.join(List) 12 print(NewStr) #aa and bb and cc and dd 13 14 mystr = " hello world and itcast and itheima and Python " 15 16 # 1、capitalize() 字符串首字母大写 17 new_str = mystr.capitalize() 18 print(new_str) 19 # 2.title(): 字符串中每个单词首字母大写 20 new_str = mystr.title() 21 print(new_str) 22 # 3. upper():小写转大写 23 new_str = mystr.upper() 24 print(new_str) 25 # 4. lower(): 大写转小写 26 new_str = mystr.lower() 27 print(new_str) 28 29 # 1. lstrip(): 删除左侧空白字符 30 new_str = mystr.lstrip() 31 print(new_str) 32 # 2. rstrip(): 删除右侧空白字符 33 new_str = mystr.rstrip() 34 print(new_str) 35 # 3.strip():删除两侧空白字符 36 new_str = mystr.strip() 37 print(new_str) 38 39 ‘‘‘ 输出 40 hello world and itcast and itheima and python 41 Hello World And Itcast And Itheima And Python 42 HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON 43 hello world and itcast and itheima and python 44 hello world and itcast and itheima and Python 45 hello world and itcast and itheima and Python 46 hello world and itcast and itheima and Python 47 48 ‘‘‘
对其操作:
1 >>> mystr=‘hello‘ 2 >>> mystr.ljust(10) 3 ‘hello ‘ 4 >>> mystr.rjust(10) 5 ‘ hello‘ 6 >>> mystr.ljust(10,‘.‘) 7 ‘hello.....‘ 8 >>> mystr.rjust(10,‘.‘) 9 ‘.....hello‘ 10 >>> mystr.center(10) 11 ‘ hello ‘
在python中判断操作一般常用的有字母判断、数字判断、数字字母组合判断、空格判断。
1 mystr = "hello world and itcast and itheima and Python" 2 3 # 1. startswith(): 判断字符串是否以某个子串开头 4 print(mystr.startswith(‘hello‘)) #T 5 print(mystr.startswith(‘hel‘)) #T 6 print(mystr.startswith(‘hels‘)) #F 7 8 9 # 2. endswith(): 判断字符串是否以某个子串结尾 10 print(mystr.endswith(‘Python‘)) #T 11 print(mystr.endswith(‘Pythons‘)) #F 12 13 14 # 3. isalpha(): 字母 15 print(mystr.isalpha()) #F 16 17 # 4. isdigit(): 数字 18 print(mystr.isdigit()) #F 19 mystr1 = ‘12345‘ 20 print(mystr1.isdigit()) #T 21 22 # 5. isalnum() : 数字或字母或组合 23 print(mystr1.isalnum()) #T 24 print(mystr.isalnum()) #F 25 mystr2 = ‘abc123‘ 26 print(mystr2.isalnum()) #T 27 28 29 # 6.isspace(): 空白 30 print(mystr.isspace()) #F 31 mystr3 = ‘ ‘ 32 print(mystr3.isspace()) #T
原文:https://www.cnblogs.com/lzy820260594/p/11789361.html