Skip to content

Instantly share code, notes, and snippets.

@myoffe
Created March 11, 2011 19:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save myoffe/866435 to your computer and use it in GitHub Desktop.
Normalize whitespace in string
def normalize_whitespace(str):
import re
str = str.strip()
str = re.sub(r'\s+', ' ', str)
return str
@myoffe
Copy link
Author

myoffe commented Mar 11, 2011

Trims whitespace from both sides and replaces all whitespace characters with a single space.
Example: " this is spacious ! " => "this is spacious !"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment