find函数:检测字符串是否包含子字符串sub,如果包含子字符串则返回开始的索引值,否则返回-1
语法格式如下:
str.find(sub[,start[,end]]) sub:指定检索的字符串 start:开始索引,默认为0 end:结束索引,默认为字符串的长度
例如:
A1=‘hello world itheima and itheimaApp’
A2=A1.find(“itheima”)
print(A2)
运行结果为:12
index函数:与find函数功能相同,只不过是sub不在字符串中,会报错
语法如下:
str.index(sub,[start[,end]])
count函数:用于统计字符串中sub子串出现的次数,结果返回为子字符串在字符串中出现的次数
语法如下:
str.count(sub,[start[,end]])
例如:
A1=‘hello world itheima and itheimaApp’
A3=A1.count(“itehima”)
print(A3)
运行结果为:2
replace函数:把旧的字符串替换成新字符串,结果是旧的字符串替换新的字符串后生成的新字符串
语法格式如下:
str.replace(old,new[,count]) count:可选参数,替换不超过count次
例如:
A1=‘hello world itheima and itheimaApp’
A4=A1.replace(‘ithe