count("x")统计字符串的元素的个数 a = "hello kitty" print (a.count("t"))#统计t出现的个数 输出结果: 2
endswith("x")判断以某个字符结尾 a = "python" print (a.endswith("n")) print (a.endswith("on")) print (a.endswith("hon")) print (a.endswith("nn")) 输出结果: True True True False
startswith("x")判断以某个字符开头 a = "python" print (a.startswith("p")) print (a.startswith("py")) print (a.startswith("pyt")) print (a.startswith("pp")) 输出结果: True True True False
capitalize()把字符串首写字母大写 a = "python" print (a.capitalize()) 输出结果: Python
upper()将字符串里的小写变成大写 a = "python" print (a.upper()) 输出结果: PYTHON
lower()把字符串大写变成小写 a = "PYTHON" print (a.lower()) 输出结果: python
swapcase()把字符串里的大小写反转 a = "PyThoN" print (a.swapcase()) 输出结果: pYtHOn
isupper()判断字符串是否大写 a = "python" a1 = "PYTHON" print (a.isupper()) print (a1.isupper()) 输出结果: False True
islower()判断字符串是否小写 a = "python" a1 = "PYTHON" print (a.islower()) print (a1.islower()) 输出结果: True False
isalnum()-判断字符串是否是字母或者数字 a = "python" a1 = "123" a2 = "python123" a3 = "中国" a4 = "#$###" print (a.isalnum()) print (a1.isalnum()) print (a2.isalnum()) print (a3.isalnum()) print (a4.isalnum()) 输出结果: True True True True False
istitle()判断首写字母是否为大写 a = "python" a1 = "Python" a2 = "PYTHON" print (a.istitle()) print (a1.istitle()) print (a2.istitle()) 输出结果: False True Fals
strip()把字符串左右空格去掉 a = " python " print (a.strip()) 输出结果: python #已经将左右空格去掉了,这样看不出来 ---------------------- strip()也会把\n换行去掉 a = "python\n" a1 = "hello" print (a.strip()) #strip()把换行去掉了 print (a1) 输出结果: python hello ---------------------- a = "python\n" a1 = "hello" print (a)#不使用strip(),将会换行 print (a1) 输出结果: python hello
lstrip()去掉字符串左空格 a = " python " print (a.strip()) 输出结果: python #最后面这有空格,这样看不出来
rstrip()去掉字符串右空格 a = " python " print (a.rstrip()) 输出结果: python #已经将右边空格去掉了,这看不出来
replace(old,new) 把字符串的内容替换掉 a = "my tang" print (a.replace("tang", "guo")) 输出结果: my guo
split(分割符,分割次数)对字符串进行各种分割 a = "my \ntang guo\nli" print (a.split())#不指定以什么分割的情况下,会默认所有的空格,换行,制表符分割 输出结果: [‘my‘, ‘tang‘, ‘guo‘, ‘li‘] ------------------------------------- a = "my tang guo" print (a.split(" ")) #以空格进行分割 输出结果: [‘my‘, ‘tang‘, ‘guo‘] -------------------------------------- a = "my tang guo li" print (a.split(" ",2))#以空格进行分割2次 输出结果: [‘my‘, ‘tang‘, ‘guo li‘] --------------------------------------- a = "my tang guo li" print (a.split("a"))#以 a进行分割,a会被切掉 输出结果: [‘my t‘, ‘ng guo li‘] -------------------------------------- 可以指定多个字符进行分割 a = "my tang guo li" print ((a.split("an"))#以 an进行分割,an会被切掉 ["my t" ,"g guo li"] ----------------------------------------
isdigit()判断字符串是否为整型 a = "python" a1 = "123" print (a.isdigit()) print (a1.isdigit()) 输出结果: True False
rfind("x")查找元素所在的索引位置,从右向左开始找,找到的第一个元素位置 a = "htp" a1 = "http" print (a.rfind("t")) print (a1.rfind("t")) 输出结果: 1 2 ----------------------------------------------------------------- find("x")从左向右开始找元素,找到的第一个位置,并返回索引位置 a = "htp" a1 = "http" print (a.find("t")) print (a1.find("t")) 输出结果: 1 1
index("x")从左向右开始找元素的位置, a = "ppython" print (a.index("p")) #有多个相同元素时,返回的是第一个找到的索素值 输出结果: 0
find 与 index看上去功能一样,其实是有区别的
a = "python"
print (a.index(x))
index 在没有找到子串的时候会报错(ValueError: substring not found),影响程序执行。
print (a.find("x"))
find 在没有找到子串时,不会报错,而是会返回-1所以不会影响执行。