import re before = "If the the problem is textual, use the the re module" after = re.sub("the the", "the", before) print(after)