import re s = '\n \t this is a string with a lot of whitespace\t' s = re.sub('\s+', '', s)
>>> s = " \t foo \n bar " >>> "".join(s.split()) 'foobar'