import re line = 'your text here' line = re.sub(r'([a-zA-z0-9 ])([?,.!:\'\"])([a-zA-z0-9 ]|$)', r'\1 \2 \3', line) line = re.sub(r'([a-zA-z0-9 ])([?,.!:\'\"])([a-zA-z0-9 ]|$)', r'\1 \2 \3', line) line = re.sub(r'[ ]+', ' ', line) print(line)