I would like to write a function that counts all non-overlapping occurences of a substring in a string. This is what I have so far:
def count(substr,theStr):
count = 0
for i in range(len(theStr)):
if theStr[i:i+len(substr)] == substr:
count = count + 1
return count
As one can see, my function only counts occurences of a string, but not non-overlapping occurences. For example, the inputs "ana" and "Banana" would yield a count of 2, even though there is only one non-overlapping instance of "ana" in "Banana". How can I extend my function so that it works correctly?