Python String count() Metoda
Przykład
Zwróć liczbę wystąpień wartości „jabłko” w ciągu:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x)
Definicja i użycie
Metoda count()
zwraca liczbę wystąpień określonej wartości w ciągu.
Składnia
string.count(value, start, end)
Wartości parametrów
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 |
Więcej przykładów
Przykład
Szukaj od pozycji 10 do 24:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple",
10, 24)
print(x)