Python基礎——count()方法
count() 方法用於統計字符串裏某個字符串出現的次數。
語法
str.count(sub, start= 0,end=len(string))
sub —— 搜索的子字符串
start —— 字符串開始搜索的位置。默認爲第一個字符,第一個字符索引值爲 0 。
end —— 字符串中結束搜索的位置。字符串中第一個字符的索引爲 0 。默認爲字符串的最後一個位置。
返回值
該方法返回子字符串在字符串中出現的次數。
示例
str = "this is string example...wow!!!"
print(str.count("i"))
print(str.count("i",4,40))
print(str.count("wow"))