import re _, count = re.subn(r'hello', '', astring, flags=re.I) print('Found', count, 'occurrences of "hello"')