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