字符串
查找
count(sub[,start[,end]])
find(sub[,start[,end]]) rfind(sub[,start[,end]])
index(sub[,start[,end]]) rindex(sub[,start[,end]])
x = "上海自来水来自海上"
x.count("海")
2
x.count("海", 0, 5)
1
x.find("海")
1
x.rfind("海")
7
x.find("贵")
-1
x.rfind("贵")
-1
x.index("贵")
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
x.index("贵")
ValueError: substring not found
替换
expandtabs([tabsize=8])
replace(old,new, count=-1)
translate(table)
code = """
print("I love python")
print("I love python")"""
new_code = code.expandtabs(4)
print(new_code)
print("I love python")
print("I love python")
"在吗!我在你家楼下,快点下来!!!".replace("在吗", "想你")
'想你!我在你家楼下,快点下来!!!'
table = str.maketrans("ABCDEFG", "1234567")
"I love python".translate(table)
'I love python'
"I love Python".translate(table)
'I love Python'
"I love FishC".translate(table)
'I love 6ish3'
"I love FishC".translate(str.maketrans("ABCDEFG", "1234567"))
'I love 6ish3'
"I love FishC".translate(str.maketrans("ABCDEFG", "1234567", "love"))
'I 6ish3'
判断
标签:love,sub,Python,end,笔记,python,DAY11,table,translate From: https://blog.csdn.net/Tulp123/article/details/141306013