In order to solve this problem, we can use find() function in Python. It returns the start position of the first occurrence of substring in the given string, then we increment this position by 1 and continue the search from that position till the end of the string.,Note that in Python, the count() function returns the number of substrings in a given string, but it does not give correct results when two occurrences of the substring overlap. Consider this example –,Given a string and a sub-string, the task is to get the count of overlapping substring from the given string.,WriteCome write articles for us and get featuredPracticeLearn and code with the best industry expertsPremiumGet access to ad-free content, doubt assistance and more!JobsCome and find your dream job with usGeeks DigestQuizzesGeeks CampusGblog ArticlesIDECampus Mantri
Output:
2
3
3
Well, this might be faster since it does the comparing in C:
def occurrences(string, sub):
count = start = 0
while True:
start = string.find(sub, start) + 1
if start > 0:
count += 1
else:
return count
>>>
import re
>>>
text = '1011101111' >>>
len(re.findall('(?=11)', text))
5
If you didn't want to load the whole list of matches into memory, which would never be a problem! you could do this if you really wanted:
>>> sum(1
for _ in re.finditer('(?=11)', text))
5
As a function (re.escape
makes sure the substring doesn't interfere with the regex):
>>> def occurrences(text, sub):
return len(re.findall('(?={0})'.format(re.escape(sub)), text))
>>>
occurrences(text, '11')
5
You can also try using the new Python regex module, which supports overlapping matches.
import regex as re
def count_overlapping(text, search_for):
return len(re.findall(search_for, text, overlapped = True))
count_overlapping('1011101111', '11') # 5
Python's str.count
counts non-overlapping substrings:
In[3]: "ababa".count("aba")
Out[3]: 1
In[10]: re.findall("a(?=ba)", "ababa")
Out[10]: ['a', 'a']
Generate all substrings
In[11]: data = "ababa"
In[17]: sum(1
for i in range(len(data)) if data.startswith("aba", i))
Out[17]: 2
def count_substring(string, sub_string):
count = 0
for pos in range(len(string)):
if string[pos: ].startswith(sub_string):
count += 1
return count
A fairly pythonic way would be to use list comprehension here, although it probably wouldn't be the most efficient.
sequence = 'abaaadcaaaa' substr = 'aa' counts = sum([ sequence.startswith(substr, i) for i in range(len(sequence)) ]) print(counts) # 5
In this tutorial, we will learn to count the number of overlapping substrings in a given string in Python., How to concatenate string with integer in Python,This is how we calculate the overlapping substrings., String Rotation using String Slicing in Python
Let us see if this works.
def overlapCount(string, substr):
count = string.count(substr)
return count
print("The count is: ", overlapCount("thatthathat", "that"))
def overlapCount(string, substr): count = string.count(substr) return count print("The count is: ", overlapCount("thatthathat","that"))
Output: The count is: 2
Let us understand this by code.
def frequencyCount(string, substr):
count = 0
pos = 0
while (True):
pos = string.find(substr, pos)
if pos > -1:
count = count + 1
pos += 1
else:
break
return count
print("The count is: ", frequencyCount("thatthathat", "that"))
Steps to find the number of overlapping occurrences,Python String - Find the number of overlapping occurrences of a substring,Example 1: Number of overlapping occurrences of substring,Counter would have the number of occurrences of a substring in the string.
Python Program
string = 'abcdefghghghghghgh.'
substring = 'ghg'
count = 0
start = 0
if (len(string) > 0 and len(string) < 201):
for i in range(len(string)):
i = string.find(substring, start)
if (i > 0):
start = i + 1
count += 1
else:
break
print(count)
Output
5
Post date April 7, 2022 ,© 2022 The Web Dev
For instance, we write
def occurrences(string, sub):
count = start = 0
while True:
start = string.find(sub, start) + 1
if start > 0:
count += 1
else:
return count
Python’s String class contains a method to count the non overlapping occurrences of a sub-string in the string object i.e. ,Let’s use the same function frequencyCountAndPositions() to find the nth occurrence of a sub-string in another string i.e. ,As, string.count() can not find the overlapping occurrences of a sub-string. So, let’s create a function to do this, ,In this article we will discuss different ways to count occurrences of a sub-string in another string and also their index positions.
Python’s String class contains a method to count the non overlapping occurrences of a sub-string in the string object i.e.
string.count(s, sub[, start[, end]])
mainStr = 'This is a sample string and a sample code. It is very short.' # Get the occurrence count of sub - string in main string. count = mainStr.count('sample') print("'sample' sub string frequency / occurrence count : ", count)
We can easily get the occurrence count using python regex too. For that we will create a regex pattern with sub-string and then find all matches of that regex pattern in another string i.e.
# Create a Regex pattern to match the substring regexPattern = re.compile("sample") # Get a list of strings that matches the given pattern i.e.substring listOfMatches = regexPattern.findall(mainStr) print("'sample' sub string frequency / occurrence count : ", len(listOfMatches))
As, string.count() can not find the overlapping occurrences of a sub-string. So, let’s create a function to do this,
'' '' Find occurrence count of overlapping substrings. Start from left and start searching for the substring when found increment the counter and keep on search from next index position. '' ' def frequencyCount(mainStr, subStr): counter = pos = 0 while (True): pos = mainStr.find(subStr, pos) if pos > -1: counter = counter + 1 pos = pos + 1 else: break return counter
Using Regex find all the matches of a sub-string in another main string and iterate over all those matches to find their index positions i.e.
# Create a Regex pattern to match the substring regexPattern = re.compile('sample') # Iterate over all the matches of substring using iterator of matchObjects returnes by finditer() iteratorOfMatchObs = regexPattern.finditer(mainStr) indexPositions = [] count = 0 for matchObj in iteratorOfMatchObs: indexPositions.append(matchObj.start()) count = count + 1 print("Occurrence Count of substring 'sample' : ", count) print("Index Positions of 'sample' are : ", indexPositions)