str1 ="hello and world and puber and python"# find 字符串序列.find(子串, 开始位置下标, 结束位置下标),没有就返回-1print(str1.find('and'))# 6print(str1.find('and',8,20))# 16print(str1.find('add'))# -1# index 字符串序列.find(子串, 开始位置下标, 结束位置下标),没有就报错print(str1.index('ands'))# count 字符串序列.count(⼦子串, 开始位置下标, 结束位置下标)print(str1.count('and'))# 3
4、修改
# 修改# repalce 替换,返回新的字符串,原有字符串不改变# 字符串序列.replace(旧⼦子串, 新⼦子串, 替换次数)
str1 ="hello and world and puber and python"
str2 = str1.replace('and','he')print(str2)
str3 = str1.replace('and','he',2)# hello he world he puber and pythonprint(str3)
str4 = str1.replace('and','he',5)# hello he world he puber he pythonprint(str4)