Count overlapping substring in a string

Question:

Say I have string = 'hannahannahskdjhannahannah' and I want to count the number of times the string hannah occurs, I can’t simply use count, because that only counts the substring once in each case. That is, I am expecting to return 4 but only returns 2 when I run this with string.count('hannah').

Asked By: Ryan Drake

||

Answers:

Don’t want to answer this for you as it’s simple enough to work out yourself.

But if I were you I’d use the string.find() method which takes the string you’re looking for and the position to start looking from, combined with a while loop which uses the result of the find method as it’s condition in some way.

That should in theory give you the answer.

Answered By: NDevox

You could use a running index to fetch the next occurance:

bla = 'hannahannahskdjhannahannah'
cnt = 0
idx = 0
while True:
    idx = bla.find('hannah', idx)
    if idx >= 0:
        cnt += 1
        idx += 1
    else:
        break
print(cnt)

Gives:

>> 4
Answered By: RickyA

How about something like this?

>>> d = {}
>>> string = 'hannahannahskdjhannahannah'
>>> for i in xrange(0,len(string)-len('hannah')+1):
...     if string[i:i+len('hannah')] == 'hannah':
...             d['hannah'] = d.get('hannah',0)+1
... 
>>> d
{'hannah': 4}
>>> 

This searches the string for hannah by splicing the string iteratively from index 0 all the way up to the length of the string minus the length of hannah

Answered By: Harpal
'''
s: main string
sub: sub-string
count: number of sub-strings found
p: use the found sub-string's index in p for finding the next occurrence of next sub-string
'''
count=0
p=0
for letter in s:
    p=s.find(sub,p)   
    if(p!=-1):
        count+=1
        p+=1
print count
Answered By: gopi m

If you want to count also nonconsecutive substrings, this is the way to do it

def subword(lookup,whole):
    if len(whole)<len(lookup):
          return 0
    if lookup==whole:
          return 1
    if lookup=='':
          return 1
    if lookup[0]==whole[0]:
         return subword(lookup[1:],whole[1:])+subword(lookup,whole[1:])
    return subword(lookup,whole[1:])
Answered By: Uri Goren
def Count_overlap(string, substring):   
    count = 0
    start = 0
 
    while start < len(string):
        pos = string.find(substring, start)
  
        if pos != -1:
            start = pos + 1
            count += 1
        else:
            break
    return count
string = "hannahannahskdjhannahannah"
print(Count_overlap(string, "hannah"))
Answered By: Vedant Bahel
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.