Python String count() Method

Returns the number of times a specific character or string appears in another string. The count function can take an optional start and end index into the string.


string.count(value, start = None, end = None)


value Required. The string value to search for within the given string
start Optional. The index at which to start searching. The default is 0.
end Optional. The index at which to stop searching. The default is the end of the string.


str = 'The quick brown fox jumped over the lazy dog'
occurances = str.count('o')
print(f'The letter "o" occurs {occurances} times')

occurances = str.count('the')
print(f'The word "the" occurs {occurances} times')

# first 10 letters of the string:
occurances = str.count('the', 0, 10)
print(f'The word "the" occurs {occurances} times')


The letter "o" occurs 4 times
The word "the" occurs 1 times
The word "the" occurs 0 times

Notes: The count function is case-sensitive.