Метод подсчета строк Python ()
Пример
Возвращает количество раз, когда значение «яблоко» встречается в строке:
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)