1.find string = "Beautiful is better than ugly " 检查字符串是否包含在string中,如果包含则返回字符串开始的下标(索引),如果不包含则返回-1
>>> string='Beautiful is better than ugly '
>>> string.find('better')
13
>>> string.find('python')
-1
rfind 和find类似,不过是从右侧开始查找,因为有的字符串在原字符串中会有多个,但返回值只有第一个,所有rfind是属于右侧优先原则
2.index 作用和find一样,但如果不包含字符串则抛异常(报错),rindex和rfind类似
>>> string='Beautiful is better than ugly '
>>> string.index('better')
13
>>> string.index('python')
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
string.index('python')
ValueError: substring not found
3.count 返回字符串在目标字符串中出现的次数
>>> string='Beautiful is better than ugly '
>>> string.count('a')
2
>>> string.count('better')
1
>>> string.count(' ')
5
>>> string.count('python')
0
4.replace 将字符串中指定的字符串用其它字符串进行替换
>>> string='Beautiful is better than ugly '