Python String count() Methode
Voorbeeld
Retourneer het aantal keren dat de waarde "appel" in de tekenreeks voorkomt:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x)
Definitie en gebruik
De count()
methode retourneert het aantal keren dat een opgegeven waarde in de tekenreeks voorkomt.
Syntaxis
string.count(value, start, end)
Parameterwaarden
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 |
Meer voorbeelden
Voorbeeld
Zoeken van positie 10 tot 24:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple",
10, 24)
print(x)