[Tutor] function for removing all white spaces from a string

Tim timmichelsen at gmx-topmail.de
Mon Oct 1 18:37:59 CEST 2007


Hello,
after reading the responses to athread in 2004 [1] I am wondering why there is
no easy function in python to remove all white spaces from a string.

Like:

" i am very fine "
to
"iamveryfine"

In IDL there's just one simple function which does this: STRCOMPRESS [2].

Is there really not yet such a function in Python?

If not I created it:

a = " i am very fine "


def strcompress(mystring):
...     mystring_compressed = ''.join(mystring.split())
...     return mystring_compressed

 strcompress(a)
'iamveryfine'

Thanks for your input,
Timmie

[1] Re: how to strip whitespaces from a string.
http://article.gmane.org/gmane.comp.python.tutor/18622

[2] STRCOMPRESS http://idlastro.gsfc.nasa.gov/idl_html_help/STRCOMPRESS.html



More information about the Tutor mailing list