[Tutor] how to strip whitespaces from a string.
Jacob S.
keridee at jayco.net
Mon Oct 11 23:52:45 CEST 2004
Hey guys.
I have one more way to do it.
>>> import string
>>> s = "I have learned some python"
>>> s = string.replace(s," ","")
>>> s
'Ihavelearnedsomepython'
Or you could do it this way.
>>> s = "I have learned some python"
>>> s = s.replace(" ","")
>>> s
'Ihavelearnedsomepython'
>>> # Notice no import string call
>>>
Or, if we wish to replace all of the whitespace charaters, which I
believe would be best, we:
>>> import string
>>> s = "I have learned\tsome python"
>>> for x in string.whitespace:
. . . s = s.replace(x,"")
. . .
>>> s
'Ihavelearnedsomepython'
There are other ways as well. That's okay though! I don't have time to
say them now.
As always, hope this helps,
Jacob Schmidt
More information about the Tutor
mailing list