[Tutor] how to strip whitespaces from a string.
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()
> >'I am learning python'
> >How can I get the ouput to:
> How about this
> s=" I am Learning Python "
> s="".join([x.strip() for x in s.split(' ')])
> print s
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() )
> Tutor maillist - Tutor at python.org
More information about the Tutor