Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (45.3k points)
edited by

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...

1 Answer

0 votes
by (16.8k points)

Try as shown below:

>>> import re

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

'The quick brown fox'

Related questions

+1 vote
1 answer
+2 votes
1 answer
0 votes
1 answer
0 votes
2 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...