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?
>>> import re >>> re.sub(' +', ' ', 'The quick brown fox') 'The quick brown fox'
foo
is your string:
" ".join(foo.split())
Be warned though this removes "all whitespace characters (space, tab, newline, return, formfeed)" (thanks to hhsaffar, see comments). I.e., "this is \t a test\n"
will effectively end up as "this is a test"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With