def find_all(a_str, sub):
    start = 0
while True:
    start = a_str.find(sub, start)
if start == -1:
    return
yield start
start += len(sub)
list(find_all('spam spam spam spam', 'spam'))
Find all occurrences of a substring in Python