代码展示
# find() :检测某个字串是否包含在某个字符串中,如果在则返回这个子串开始位置的下标,如果不在则返回-1.
# 格式 :字符串序列.find(子串,开始位置下标,结束位置下标)
str1="python helloworld,welcome to China,python"
print(str1.find("python"))
print(str1.find("python",28,35))
print(str1.find("python",30))
print(str1.rfind("python"))
代码展示
# index() :检测某个字串是否包含在某个字符串中,如果在则返回这个字串开始位置的下标,如果不在则报错
# 格式 :字符串序列.index(子串,开始位置下标,结束位置下标)
str2=" helloworld,welcome to China,python"
print(str2.index("python")) # 29
print(str2.index("python",10,50)) # 29
# print(str2.index("python",70)) #报错
print(str2.rindex("python")) # 29
额外功能
代码展示
# count() :返回子串在某个字符串中出现的次数
# 格式 :字符串序列.count(子串,开始位置下标,结束位置下标)
str3="wo ai java and python and"
print(str3.count("and")) # 2
print(str3.count("and",13,35)) # 1
print(str3.count("and",30)) # 0