Possible Duplicate:
Substitute multiple whitespace with single whitespace in Python
How do I compress multiple whitespaces to 1 whitespace, in python?
For example, let's say I have a string
"some user entered text"
and I want that to become
"some user entered text"
' '.join("some user entered text".split())
>>> import re
>>> re.sub("\s+"," ","some user entered text")
'some user entered text'
>>>
EDIT:
This will also replace line breaks and tabs etc.
If you specifically want spaces / tabs you could use
>>> import re
>>> re.sub("[ \t]+"," ","some user entered text")
'some user entered text'
>>>
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