How can I count the number of times a given substring is present within a string in Python? For example: >>> 'foo bar foo'.numberOfOccurrences('foo') 2 To get indices of the substrings, see How to find all occurrences of a substring?. Continue reading...