Wrong result in str.count() method

Question:

I’m confusing with a very simple string count operation:

s = 'BANANA'
s.count('ANA')

This should result in 2, right? Since the substring, ANA appears 2 times in BANANA.

But I’ve got 1 as a result.

>>> s = 'BANANA'
>>> s.count('ANA')
1

No idea why the wrong result. It is such a simple operation!

Appreciate any help.


PS: How can I solve this problem?

Asked By: Henrique Branco

||

Answers:

Good question. But Python string count() doesn’t “backtrack”. Once it finds the first “ANA”, it looks forward through the remaining two letters” “NA”.

This kind of “forward search” is the same as most programming languages, for example Java indexOf(), C strstr() and VB.Net InStr()

Answered By: FoggyDay

string.count() does not count the overlapping occurrences.

If you would like to count overlapped occurrences, a simple loop over the string will count it:

s = 'BANANA'
i = 0
cnt = 0
while True:
    i = s.find('ANA', i)
    if i >= 0:
        i += 1
        cnt += 1
    else:
        break

Alternatively you can use regex too as in @Henrique’s answer below.

Answered By: Ehsan

In ‘BANANA’, there is only a single complete ‘ANA’. Count() is returning 1 because after it finds ‘ANA’, all that is left is ‘NA’.

Answered By: Joseph Julian

Solved the problem using the new regex library. It has a new parameter overlapped — extremely useful.

>>> import regex as re
>>> len(re.findall("ANA", "BANANA", overlapped=True))
2

I found the solution at this question here in SO.

Answered By: Henrique Branco
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.