Python字符串常见操作

 先初始化一个字符串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










猜你喜欢

转载自blog.csdn.net/u012236875/article/details/75507924