How can I count the number of times a given substring is present within a string in Python?
For example:
>>> \'foo bar foo\'.numberOfOccurre
If you want to count all the sub-string (including overlapped) then use this method.
import re def count_substring(string, sub_string): regex = '(?='+sub_string+')' # print(regex) return len(re.findall(regex,string))