本文原创,版权属作者个人所有,如需转载请联系作者本人。Q&微:155122733
--------------------------------------------------------------------------------------------------------
1 find(str) :检测str是否包含在mystr中,是返回开始的索引值,否返回-1;
完整结构:mystr.find(str,start = 0, end=len(mystr)) str代表搜索的字符,start代表搜索的起>始位置,end代表搜索的结束位置(请注意不是长度,是下标)
mystr = 'hello world python itcast I and you itcastcpp' print(mystr.find('it'))#display 19, return 从左至右第一次出现的字符的下标 print(mystr.find("it", 22,200))#display 36 print(mystr.find("it", 22,24))#display -1 在指定范围内没有找到字符串则返回-1
2 index()
跟find()方法一样,只是如果str不在会报一个异常
3 rfind()和find()类似,只是从右边开始找
4 rindex()方法和index()方法类似,只是从右边开始找
5 count(str):返回str出现的次数
完整结构: mystr.count(str,start = 0, end=len(mystr)) 返回str在star和end之间出现的次数
mystr = 'hello world python itcast I and you itcastcpp'
print(mystr.count('it'))#display 2
6 mystr.replace(str1,str2, mystr.count(str1)) 把mystr中的str1替换成str2,如果count指定,则替换不超过count次
mystr = 'hello world python itcast I and you itcastcpp'
print(mystr.replace('it','IT')) #"display hello world python ITcast I and you ITcastcpp"
注意:此时只要没有重新给mystr赋值,则mystr并不改变。
7 mystr.split("切割字符",切割几次) 如果切割字符和切割几次不设置,默认以空格切割,切割全部
mystr = 'hello world python itcast I and you itcastcpp'
print(mystr.split())#display ['hello', 'world', 'python', 'itcast', 'I', 'and', 'you', 'itcastcpp'] print(mystr.split("o",2))#display ['hell', ' w', 'rld python itcast I and you itcastcpp']
8 capitalize() 把字符串的第一个字符大写
mystr = 'hello world python itcast I and you itcastcpp'
print(mystr.capitalize())#diplay Hello world python itcast i and you itcastcpp
9 title() 把字符串的每个单词首字母大写
mystr = 'hello world python itcast I and you itcastcpp'
print(mystr.title())#display Hello World Python Itcast I And You Itcastcpp
10 lower() 把字符串的每个字符小写
11 upper() 把字符串的每个字符大写
12 startswith(obj) 检查字符串是否以obj开头,是则返回True,否则返回False
mystr = 'hello world python itcast I and you itcastcpp'
print(mystr.startswith("hello"))#display True print(mystr.startswith("Hello"))#display False
13 endswith(obj) 检查字符串是否以obj结尾,是则返回True,否则返回False
filename = "abc.txt" print(filename.endswith(".txt"))#display True
14 ljust(width) 返回一个原字符串左对齐,并使用空格填充至长度width的新字符串
rjust(width) 返回一个原字符串右对齐,并使用空格填充至长度width的新字符串
center(width) 返回一个原字符串居中,并使用空格填充至长度width的新字符串
filename = "abc.txt" print(filename.ljust(15)) print(filename.rjust(15)) print(filename.center(15))
显示结果如下图:
15 lstrip() 删除mystr左边的空白字符
rstrip() 删除mystr右边的空白字符
strip() 删除mystr两端的空白字符, (如果括号内有字符,则删除两端的字符)
filename = " abc.txt " print(filename.lstrip()) print(filename.rstrip()) print(filename.strip())
显示如下:
16 partition(str) 把mystr以str分割成三部分,str前,str,str后
rpartition(str) 把mystr从右边开始以str分割成三部分,str前,str,str后
name = "laowang laoli laoli laoliu" print(name.partition("laoli")) #display ('laowang ', 'laoli', ' laoli laoliu')
print(name.rpartition("laoli")) #display ('laowang laoli', 'laoli', ' laoliu')
17 splitlines() 按照行分割,返回一个包含各行作为元素的列表
strlist = "hello world" print(strlist.splitlines())#['hello', 'world']
18 mystr.isalpha() 字符串是否全都是字母,如果是返回True,否返回False
mystr.isdigit() 字符串是否全都是数字,并且还得是整数,如果是返回True,否返回False
mystr.isalnum() 字符串是否全都是数字或者字母,如果是返回True,否返回False
mystr.isspace() 字符串是否全都是空格,如果是返回True,否返回False
19 join 列表中的元素用特定的字符连接成字符串
strtmp = " " li = ["My", "name", "is", "cola"] #列表 print(strtmp.join(li))#display My name is cola strtmp = "_" print(strtmp.join(li))#display My_name_is_cola