I want to find all the counts (overlapping and non-overlapping) of a sub-string in a string. I found two answers one of which is using regex which is not my intention and t
Does this do the trick?
def count(string, substring): n = len(substring) cnt = 0 for i in range(len(string) - n): if string[i:i+n] == substring: cnt += 1 return cnt print count('ababaa', 'aba') # 2
I don't know if there's a more efficient solution, but this should work.