I\'m trying to find the number of times \'bob\' occurs in a string of characters like \'abdebobdfhbobob\'.
My code (that I found through another stackoverflow question)
We can just check all possible candidates:
def count_substrings(sub, main): n = len(sub) return sum(sub == main[i : i+n] for i in range(len(main) - n + 1)) s = 'abdebobdfhbobob' sub = 'bob' print('The number of times %s occurs is: %d' % (sub, count_substrings(sub, s))) # 3