Python String count() Methode
Beispiel
Gibt zurück, wie oft der Wert „apple“ in der Zeichenfolge vorkommt:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x)
Definition und Verwendung
Die count()
Methode gibt zurück, wie oft ein bestimmter Wert in der Zeichenfolge vorkommt.
Syntax
string.count(value, start, end)
Parameterwerte
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 |
Mehr Beispiele
Beispiel
Suche von Position 10 bis 24:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple",
10, 24)
print(x)