Metoda Python String count() .
Příklad
Vrátí, kolikrát se v řetězci objeví hodnota „apple“:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x)
Definice a použití
Metoda count()
vrací, kolikrát se zadaná hodnota objevila v řetězci.
Syntax
string.count(value, start, end)
Hodnoty parametrů
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 |
Další příklady
Příklad
Hledat od pozice 10 do 24:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple",
10, 24)
print(x)