TIMEX TIMEX - 3 months ago 16
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
>>> import re
>>> re.sub(' +',' ','The     quick brown    fox')
'The quick brown fox'