先初始化乙個字串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:
print(scstring.count("shen"))
# 輸出結果,shen在字串中的個數
3
replace:
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
Python字串常見操作
如有字串mystr hello world hello everyone 以下是常見的操作 1 find與index 檢測 str 是否包含在 mystr中,如果是返回開始的索引值,否則find返回 1,index將報錯。返回 str 在 mystr裡面出現的次數,可以指定查詢範圍。把 mystr ...
python中字串常見操作
mystr hello world,this is python 1 find 檢測 str 是否包含在 mystr中,如果是返回開始的索引值,否則返回 1 mystr.find str,start 0,end len mystr mystr hello world,this is python m...
python常見字串操作
str hello world print str.upper 把所有字元中的小寫字母轉換成大寫字母 print str.lower 把所有字元中的大寫字母轉換成小寫字母 print str.capitalize 把第乙個字母轉化為大寫字母,其餘小寫 print str.title 把每個單詞的第乙...