[Tutor] how to strip whitespaces from a string.

Lloyd Kvam pythonTutor at venix.com
Fri Oct 8 22:41:14 CEST 2004


On Fri, 2004-10-08 at 14:09, Chad Crabtree wrote:
> kumar s wrote:
> 
> >Thank you. 
> > But I messed up my question. 
> >s = "   I am learning python    "
> >f = s.strip()
> >f 
> >'I am learning python'
> >
> >How can I get the ouput to:
> >
> >'Iamlearningpython'.
> >
> >  
> >
> How about this
> 
> s="   I am Learning Python    "
> s="".join([x.strip() for x in s.split(' ')])
> print s
> IamLearningPython

Note that s.split() will simply discard all whitespace and return the
words.  Then there is no need for x.strip().  The line simply becomes:
s = "".join( s.split() )

>>> s = "   I am Learning Python   "
>>> ''.join( s.split() )
'IamLearningPython'

> 
> 
> 
> 
> 		
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp



More information about the Tutor mailing list