Possible bug in str.count

Python 3.12.1
>>> s = "adada"                                                                                             
>>> s.count("ada")                                                        
>>> 1 

Is this expected behavior?

Yes, because the documentation says (emphasis added):

Return the number of non-overlapping occurrences of substring sub in the range [start, end].

1 Like

Yes, the result is the number of non-overlapping occurrences.

1 Like

Thanks, but why is it made so? Like the expected answer by common logic should be 2. What’s the motivation behind such design? Also KMP would also give 2 in O(n + m). Is there another method for count with overlap? This can cause serious bugs.

1 Like

The typical workaround is to use a lookahead regex search instead:

import re

assert len(re.findall('(?=ada)', 'adada')) == 2

Thanks for that though my question is not regarding how we can do it but rather why is such design there.

That depends on the question you ask.
If the question is “how many occurrences can be removed?”
Then 2 is the wrong answer.

4 Likes

Only if you disregard the documented behavior. If you expect overlapping matches to be counted, you need to look for a different function.

2 Likes

Thanks, I never looked from the following perspective.

One can remove simultaneously or sequentially. Both interpretations would still fit.