strip bug?

Chris Kaynor ckaynor at zindagigames.com
Tue Feb 24 18:22:06 EST 2015


On Tue, Feb 24, 2015 at 3:05 PM, <baykiwi at gmail.com> wrote:

> >>> 'http://xthunder'.strip('http://')
> 'xthunder'
> >>> 'http://thunder'.strip('http://')
> 'under'
> >>>
>
> I could understand backslash but forward slash?


I believe the issue is that str.strip does not do quite what you are
thinking it does, however your message is very unspecific about what you
expect to get. It seems that you except str.strip to remove a sub-string,
while it actually removes the list of specified characters (see
https://docs.python.org/3.4/library/stdtypes.html?highlight=str.strip#str.strip).
As such, you would get the same result from 'http://thunder'.strip('htp:/')
and 'http://thunder'.strip('/thp:') as from your samples.

To get what I am guessing you want ("xthunder" for the first and "thunder"
for the second), you should use splicing, likely combined with
str.startswith() (untested code):
url = 'http://thunder/
if url.startswith('http://'):
    url = url[7:]
else
    # Handle case without the prefix in whatever manner is correct for your
case. Possibly continue, possibly error out.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150224/8eb119a4/attachment.html>


More information about the Python-list mailing list