Is there a method in python in order to check how many times a string appears in another? For example
a="1ab1"
new_string="1ab1ab1ab1"
I should get 3 but I got 2 with str.count()
Thank you
Is there a method in python in order to check how many times a string appears in another? For example
a="1ab1"
new_string="1ab1ab1ab1"
I should get 3 but I got 2 with str.count()
Thank you
Here is a function that will count the number of substrings with overlap in a string:
def getSubstringCount(s1, s2):
pos = 0
count = 0
while pos < len(s1):
pos = s1.find(s2, pos)
if pos == -1:
break
else:
count += 1
pos += 1
return count
a="1ab1"
new_string="1ab1ab1ab1"
cnt = new_string.count(a)
print(cnt)
Output: 3