strip not working on strings?
Eric Jacoboni
jaco at neottia.net
Sun Nov 13 16:42:22 EST 2005
In article <1131916607.467166.207550 at g14g2000cwa.googlegroups.com>,
dan.j.weber at gmail.com wrote:
> I'm using Python 2.3.5 and when I type the following in the interactive
> prompt I see that strip() is not working as advertised:
>
> >>>s = 'p p:p'
> >>>s.strip(' :')
> 'p p:p'
>
> Is this just me or does it not work? I want to get rid of all ' ' and
> ':' in the string. I've checked the doc and from what I can tell this
> is what strip() is supposed to do.
In /my/ docs, s.strip return a copy of s where all /leading/ and
/heading/ spaces are removed. s.strip(x) does the same but removing
chars of x.
So, what you're asking for by s.strip(' :') is "remove heading or
leading space or ':' chars", /not/ "remove heading or leading space or
':' chars".
If you want to get rid of ' ' and ':' anywhere in s, i think that
string.maketrans and s.translate will do the job:
>>> import string
>>> s = 'p p:p'
>>> ident = string.maketrans('', '')
>>> s.translate(ident,' :')
'ppp'
--
Jaco
More information about the Python-list
mailing list