Skip to content
String Methods

count()

Count how many non-overlapping times some text appears.

str.count(sub, start=0, end=len(str))

str.count() returns the number of non-overlapping occurrences of substring sub in the range [start, end].

Parameters

NameDescription
subThe text to count.
startOptional starting position.
endOptional position where the search stops.

Returns

An integer count.

Try it

Run it and change it
text = "banana"
print(text.count("an"))

Worth knowing

  • Non-overlapping matches: 'aaaa'.count('aa') returns 2, not 3, because matched characters are not reused.

Related entries