Jan-20-2018, 04:36 AM
Welcome to the forum, please use code tags in the future around your code (the python button you see when posting).
Seems your answer is almost correct except you are setting x to 0 every time through the loop.
With that outside the loop it seems to work:
Seems your answer is almost correct except you are setting x to 0 every time through the loop.
With that outside the loop it seems to work:
x = 0 for s,_ in enumerate(haystack): haysub = haystack[s:len(needle) + s] if haysub == needle: x += 1 print(x)