Python字符串常见操作

来源:互联网 发布:windows文件系统书籍 编辑:程序博客网 时间:2024/05/21 09:10

 先初始化一个字符串scString

scString = "my name is shenchong shen shen"

find:

scString = "my name is shenchong shen shen"print(scString.find("shen"))# 输出结果,第一个shen的s的角标为1111
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










原创粉丝点击