Using Python 2.7, I was attempting to count the number of occurances of \'bob\' in the phrase \'bobbbobobboobobookobobbobbboj.\' To do this, I wrote the code below:
>>> s = 'bobbbobobboobobookobobbobbboj' >>> term = 'bob' sum(1 for i, j in enumerate(s) if s[i:i+len(term)] == term) 6