HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonCritical

Is there a simple way to remove multiple spaces in a string?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
theremultiplesimpleremovewaystringspaces

Problem

Suppose this string:

The fox jumped over the log.


Turning into:

The fox jumped over the log.


What is the simplest (1-2 lines) to achieve this, without splitting and going into lists?

Solution

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

Code Snippets

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

Context

Stack Overflow Q#1546226, score: 944

Revisions (0)

No revisions yet.