Python字串常見操作
阿新 • • 發佈:2019-02-10
先初始化一個字串scString
scString = "my name is shenchong shen shen"
find:
scString = "my name is shenchong shen shen"
print(scString.find("shen"))
# 輸出結果,第一個shen的s的角標為11
11
index:print(scString.index("shen"))
#輸出結果,第一個shen的s的角標
11
rfind:
print(scString.rfind("shen"))
#輸出結果,最後一個shen的s的角標
26
count:
replace:print(scString.count("shen")) # 輸出結果,shen在字串中的個數 3
print(scString.replace("shen","xjh",2))
#輸出結果,將shen替換成xjh,最後一個引數是替換字串中的個數,預設是全部替換
my name is xjhchong xjh shen
split:
print(scString.split(" "))
#輸出結果,將字串以空格切割,並且空格省去
['my', 'name', 'is', 'shenchong', 'shen', 'shen']
capitalize
title
startswith("")
endswith
lower
upper
ljust
rjust
lstrip
rstrip
strip
partition
spitlines
isalpha
isdigit
isalnum
join