Python string count() method
Count how many times some text appears (without overlapping).
Syntax
str.count(sub)
Parameters
| sub | the text to count |
Return value
int
Example
Code
print("banana".count("a")) print("banana".count("an")) text = "the cat and the hat" print(text.count("the"), len(text.split()))
Output
3 2 2 5
Related
Learn it step by step in the free lesson Strings, with exercises that check your answers.