Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94.2k users

Browse Categories