TIMEX TIMEX - 1 year ago 58
Python Question

A simple way to remove multiple spaces in a string in Python

Suppose this is the string:

The fox jumped over the log.


It would result in:

The fox jumped over the log.


What is the simplest, 1-2 liner that can do this? Without splitting and going into lists...

Answer Source
>>> import re
>>> re.sub(' +',' ','The     quick brown    fox')
'The quick brown fox'