re.sub(r'[?]+', '?', text) code example Example: re.sub in python example import re result = re.sub(pattern, repl, string, count=0, flags=0);