常用的幾個:
isdigit
replace
find
count
strip
center
split
format
join
python字符串方法
s = 'Hello world!'
-
s.capitalize()
#將字符串的第一個字母變成大寫,其他字母變小寫。
結(jié)果:Hello world!
2.print(s.casefold())
#變?yōu)樾?br>
結(jié)果:hello world!
print(s.center(50,'*'))
結(jié)果:*******************Hello world!*******************print(s.count('o'))
#幾個字母O
結(jié)果: 2
5.print(s.count('o',0,5))
#從 0 到 5 幾個O
結(jié)果: 1
6.print(s.endswith('!'))
# 是否是以!結(jié)尾
結(jié)果:True
7.print('a\tb') #\t tab
# \t tab鍵
\t.png
8.print(s.find('o'))
#只取了第一個
結(jié)果:4
-
print(s.find('o',0,3))
結(jié)果 -1
10.print(s.find('o',0,5))
結(jié)果:4
- format
s1 = "I am {0},like {1}"
print(s1.format("gudon","blue"))
結(jié)果:I am gudon,like blue
- format
s2 = "I am {name},like {color}"
print(s2.format(name = "sky",color = "green"))
結(jié)果:I am sky,like green
-
判斷是否是整數(shù)
isdecimal
isdigit
-
判斷是否是一個可用變量名
isidentifier -
是否是小寫
islower
16.是否都是大寫
isupper
17.是否全部為數(shù)字
isnumeric
18.join
join
19.ljust、rjust
ljust.png
rjust.png
20.大小寫 lower upper
lower and upper.png
21.去除多余空格等
strip
-
partition
partition -
replace
replace.png
24.split
split
-
rsplit
rsplit -
startswith 以什么開頭
startswith