Python String count() Metode
Voorbeeld
Wys die aantal kere wat die waarde "appel" in die string verskyn:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x)
Definisie en gebruik
Die count()
metode gee die aantal kere wat 'n gespesifiseerde waarde in die string verskyn.
Sintaksis
string.count(value, start, end)
Parameterwaardes
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 voorbeelde
Voorbeeld
Soek van posisie 10 tot 24:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple",
10, 24)
print(x)