Determining how many times a substring occurs in a string in Python

PythonString

Python Problem Overview


I am trying to figure out how many times a string occurs in a string. For example:

nStr = '000123000123'

Say the string I want to find is 123. Obviously it occurs twice in nStr but I am having trouble implementing this logic into Python. What I have got at the moment:

pattern = '123'
count = a = 0
while pattern in nStr[a:]:
    a = nStr[a:].find(pattern)+1
    count += 1
return count

The answer it should return is 2. I'm stuck in an infinite loop at the moment.

I was just made aware that count is a much better way to do it but out of curiosity, does anyone see a way to do it similar to what I have already got?

Python Solutions


Solution 1 - Python

Use str.count:

>>> nStr = '000123000123'
>>> nStr.count('123')
2

A working version of your code:

nStr = '000123000123'
pattern = '123'
count = 0
flag = True
start = 0

while flag:
    a = nStr.find(pattern, start)  # find() returns -1 if the word is not found, 
    #start i the starting index from the search starts(default value is 0)
    if a == -1:          #if pattern not found set flag to False
        flag = False
    else:               # if word is found increase count and set starting index to a+1
        count += 1        
        start = a + 1
print(count)

Solution 2 - Python

The problem with count() and other methods shown here is in the case of overlapping substrings.

For example: "aaaaaa".count("aaa") returns 2

If you want it to return 4 [(aaa)aaa, a(aaa)aa, aa(aaa)a, aaa(aaa)] you might try something like this:

def count_substrings(string, substring):
    string_size = len(string)
    substring_size = len(substring)
    count = 0
    for i in xrange(0,string_size-substring_size+1):
        if string[i:i+substring_size] == substring:
            count+=1
    return count

count_substrings("aaaaaa", "aaa")
# 4

Not sure if there's a more efficient way of doing it, but I hope this clarifies how count() works.

Solution 3 - Python

import re

pattern = '123'

n =re.findall(pattern, string)

We can say that the substring 'pattern' appears len(n) times in 'string'.

Solution 4 - Python

In case you are searching how to solve this problem for overlapping cases.

s = 'azcbobobegghaklbob'
str = 'bob'
results = 0
sub_len = len(str) 
for i in range(len(s)):
    if s[i:i+sub_len] == str: 
        results += 1
print (results)

Will result in 3 because: [azc(bob)obegghaklbob] [azcbo(bob)egghaklbob] [azcbobobegghakl(bob)]

Solution 5 - Python

I'm pretty new, but I think this is a good solution? maybe?

def count_substring(str, sub_str):
    count = 0
    for i, c in enumerate(str):
        if sub_str == str[i:i+2]:
            count += 1
    return count

Solution 6 - Python

string.count(substring) is not useful in case of overlapping.

My approach:

def count_substring(string, sub_string):
    
    length = len(string)
    counter = 0
    for i in range(length):
        for j in range(length):
            if string[i:j+1] == sub_string:
                counter +=1
    return counter

Solution 7 - Python

You are not changing a with each loop. You should put:

a += nStr[a:].find(pattern)+1

...instead of:

a = nStr[a:].find(pattern)+1

Solution 8 - Python

def count_substring(string, substring):
         c=0
         l=len(sub_string)
         for i in range(len(string)):
                 if string [i:i+l]==sub_string:
                          c=c+1
         return c
string=input().strip()
sub_string=input().strip()

count= count_substring(string,sub_string)
print(count)

Solution 9 - Python

As mentioned by @João Pesce and @gaurav, count() is not useful in the case of overlapping substrings, try this out...

def count_substring(string, sub_string):
    c=0
    for i in range(len(string)):
        if(string[i:i+len(sub_string)]==sub_string):
            c = c+1
    return c

Solution 10 - Python

def countOccurance(str,pat):
    count=0
    wordList=str.split()
    for word in wordList:
        if pat in word:
            count+=1
    return count

Solution 11 - Python

Usually i'm using enumerate for this kind of problems:

def count_substring(string, sub_string):
        count = 0
        for i, j in enumerate(string):
            if sub_string in string[i:i+3]:
                count = count + 1
        return count
     

Solution 12 - Python

def count(sub_string,string):

count = 0
ind = string.find(sub_string)

while True:
    if ind > -1:
        count += 1
        ind = string.find(sub_string,ind + 1)
    else:
        break
return count

Solution 13 - Python

def count_substring(string, sub_string):
    count = 0
    len_sub = len(sub_string)
    for i in range(0,len(string)):
        if(string[i:i+len_sub] == sub_string):
            count+=1
    return count

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1294377View Question on Stackoverflow
Solution 1 - PythonAshwini ChaudharyView Answer on Stackoverflow
Solution 2 - PythonJoão PesceView Answer on Stackoverflow
Solution 3 - PythonPrasannaView Answer on Stackoverflow
Solution 4 - PythonTawabGView Answer on Stackoverflow
Solution 5 - PythonmuramenaView Answer on Stackoverflow
Solution 6 - PythonGaurav ParasharView Answer on Stackoverflow
Solution 7 - PythonN PradView Answer on Stackoverflow
Solution 8 - PythonPriyanka KumariView Answer on Stackoverflow
Solution 9 - PythonAditya PatnaikView Answer on Stackoverflow
Solution 10 - PythonBhabani SharmaView Answer on Stackoverflow
Solution 11 - Pythonruddy simonpourView Answer on Stackoverflow
Solution 12 - PythonPrince_IsraelView Answer on Stackoverflow
Solution 13 - PythonAvancha BhargavaView Answer on Stackoverflow