count() method –
The count method returns the number of occurrences of a substring in a string.
string.count(substring, start, end)
Substring – the count of the substring that we want.
Start – starting index from where to start the search.
end – the end index of the string where the search ends.
Let’s say that we have a lyrics and we want to find out how many time any particular word occurs in that lyrics. To do that we can use the count method.
In : lyrics = '''And I know you'll be ok ...: And I know you'll be ok ...: And I know you'll be ok''' In : lyrics.count('ok') Out: 3
By default search starts from the beginning of the string till end. If you want, you can change that with the start and end argument.
In : lyrics.count('ok', 25, 100) Out: 2
You can also count a character.
In : lyrics.count('n') Out: 6