[Tutor] function for removing all white spaces from a string
Ricardo Aráoz
ricaraoz at gmail.com
Tue Oct 2 00:47:40 CEST 2007
Tim wrote:
> 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
''.join(c for c in a if not c.isspace())
More information about the Tutor
mailing list