Alex Rivera | Logout

Compress whitespaces in string

Asked 2011-06-27T17:54:07.650
14

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"
Edit
Report

1 Answer

2
>>> re.sub(r'\s+', ' ', 'ala   ma\n\nkota')
'ala ma kota'
answered 2011-06-27T17:56:51.380

Your Answer