count() String Method in Python with Examples

count() returns the number of non-overlapping occurrences of substring in string.

Example: count() String Method


message = 'Cat Cat Catmandu!'
substring = 'Cat'
count = message.count(substring)
print('Number of "%s" in "%s" are: %d' % (substring, message, count))

Output

Number of "Cat" in "Cat Cat Catmandu!" are: 3

Syntax: count() String Method

S.count(sub, start, end) -> int

-> int denotes it returns integer value.



Related String Methods