I need to count the nunber of times the substring \'bob\' occurs in a string.
Example problem: Find the number of times \'bob\' occurs in string s such
For this job, str.find isn't very efficient. Instead, str.count should be what you use:
>>> s = 'xyzbobxyzbobxyzbob'
>>> s.count('bob')
3
>>> s.count('xy')
3
>>> s.count('bobxyz')
2
>>>
Or, if you want to get overlapping occurrences, you can use Regex:
>>> from re import findall
>>> s = 'bobobob'
>>> len(findall('(?=bob)', s))
3
>>> s = "bobob"
>>> len(findall('(?=bob)', s))
2
>>>