1def find_all(a_str, sub):
2 start = 0
3 while True:
4 start = a_str.find(sub, start)
5 if start == -1: return
6 yield start
7 start += len(sub) # use start += 1 to find overlapping matches
8
9list(find_all('spam spam spam spam', 'spam')) # [0, 5, 10, 15]