Python 字符串 count() 方法
實例
返回值 "apple" 在字符串中出現的次數:
txt = "I love apples, apple are my favorite fruit" x = txt.count("apple") print(x)
定義和用法
count() 方法返回指定值在字符串中出現的次數。
語法
string.count(value, start, end)
參數值
參數 | 描述 |
---|---|
value | 必需。字符串。要檢索的字符串。 |
start | 可選。整數。開始檢索的位置。默認是 0。 |
end | 可選。整數。結束檢索的位置。默認是字符串的結尾。 |
更多實例
實例
從位置 10 到 24 進行檢索:
txt = "I love apples, apple are my favorite fruit" x = txt.count("apple", 10, 24) print(x)