How to get substring in string import re def count_substring(string, sub_string): result = [(_.start(), _.end()) for _ in re.finditer(sub_string, string)] return len(result[0])