Python String count()メソッド
例
値「apple」が文字列に表示される回数を返します。
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x)
定義と使用法
このcount()
メソッドは、指定された値が文字列に現れる回数を返します。
構文
string.count(value, start, end)
パラメータ値
Parameter | Description |
---|---|
value | Required. A String. The string to value to search for |
start | Optional. An Integer. The position to start the search. Default is 0 |
end | Optional. An Integer. The position to end the search. Default is the end of the string |
その他の例
例
位置10から24まで検索:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple",
10, 24)
print(x)