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

Alan Gauld alan.gauld at btinternet.com
Mon Oct 1 20:02:03 CEST 2007


"Tim" <timmichelsen at gmx-topmail.de> wrote

> 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.
>
> " i am very fine "
> to
> "iamveryfine"

You can use string.replace.

>>> 'I am very fine'.replace(' ','')
'Iamveryfine'
>>>

But you need to apply several times if you want more than simple 
spaces removed.

Or you can use regexs. (Or the translate function might work too, but 
i haven't
tried it for this)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list