xxxxxxxxxx
import re
mystr = "I want to Remove all white \t spaces, new lines \n and tabs \t"
print re.sub(r"\W", "", mystr)
Output : IwanttoRemoveallwhitespacesnewlinesandtabs
xxxxxxxxxx
#If you want to remove LEADING and ENDING spaces, use str.strip():
sentence = ' hello apple'
sentence.strip()
>>> 'hello apple'