[Tutor] how to strip whitespaces from a string.

Calum Mackay calum.mackay at cdmnet.org
Fri Oct 8 19:43:24 CEST 2004

Kent Johnson wrote:
> If you want to replace all whitespace characters then use re.sub():
>  >>> import re
>  >>> re.sub(r'\s', '', s)
> 'Iamlearningpython'

I had thought a null translation ought to be simple, but it isn't:

 >>> s.translate(string.maketrans('',''), string.whitespace)



More information about the Tutor mailing list