Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #86351 > unrolled thread

strip bug?

Started bybaykiwi@gmail.com
First post2015-02-24 15:05 -0800
Last post2015-02-27 04:18 -0600
Articles 8 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  strip bug? baykiwi@gmail.com - 2015-02-24 15:05 -0800
    Re: strip bug? Eduardo <xcesar@gmail.com> - 2015-02-24 15:15 -0800
    Re: strip bug? Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-24 16:16 -0700
    Re: strip bug? Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2015-02-25 00:19 +0100
    Re: strip bug? Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2015-02-25 00:25 +0100
    Re: strip bug? Chris Kaynor <ckaynor@zindagigames.com> - 2015-02-24 15:22 -0800
    Re: strip bug? Rustom Mody <rustompmody@gmail.com> - 2015-02-25 03:03 -0800
    Re: strip bug? babyG <bintadoma20@yahoo.com> - 2015-02-27 04:18 -0600

#86351 — strip bug?

Frombaykiwi@gmail.com
Date2015-02-24 15:05 -0800
Subjectstrip bug?
Message-ID<3a54074b-2517-472b-9fcc-50348933a959@googlegroups.com>
>>> 'http://xthunder'.strip('http://')
'xthunder'
>>> 'http://thunder'.strip('http://')
'under'
>>>

I could understand backslash but forward slash?

[toc] | [next] | [standalone]


#86354

FromEduardo <xcesar@gmail.com>
Date2015-02-24 15:15 -0800
Message-ID<411cc0ce-3df3-4e33-8853-60838c1c292b@googlegroups.com>
In reply to#86351
Well, from the docstring of strip:

------
S.strip([chars]) -> string or unicode

Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping
------

In this case 'http://thunder'.strip('http://') is the same as 'http://thunder'.strip('htp:/') and any character of the string that is passed to the method will be removed. 

[toc] | [prev] | [next] | [standalone]


#86355

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-02-24 16:16 -0700
Message-ID<mailman.19153.1424819861.18130.python-list@python.org>
In reply to#86351
On Tue, Feb 24, 2015 at 4:05 PM,  <baykiwi@gmail.com> wrote:
>>>> 'http://xthunder'.strip('http://')
> 'xthunder'
>>>> 'http://thunder'.strip('http://')
> 'under'
>>>>

This removes all leading and trailing occurrences of the characters in
the string 'http://', not the exact substring 'http://'. For that, use
either the str.replace method or slicing.

> I could understand backslash but forward slash?

I don't understand the question.

[toc] | [prev] | [next] | [standalone]


#86356

FromIrmen de Jong <irmen.NOSPAM@xs4all.nl>
Date2015-02-25 00:19 +0100
Message-ID<54ed06e1$0$2875$e4fe514c@news.xs4all.nl>
In reply to#86351
On 25-2-2015 0:05, baykiwi@gmail.com wrote:
>>>> 'http://xthunder'.strip('http://')
> 'xthunder'
>>>> 'http://thunder'.strip('http://')
> 'under'
>>>>
> 
> I could understand backslash but forward slash?
> 




>>> help("".strip)
Help on built-in function strip:

strip(...) method of builtins.str instance
    S.strip([chars]) -> str

    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove characters in chars instead.
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



It strips any chars given in the argument, not as a prefix/suffix to remove.

Irmen

[toc] | [prev] | [next] | [standalone]


#86357

FromWolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Date2015-02-25 00:25 +0100
Message-ID<mailman.19154.1424820337.18130.python-list@python.org>
In reply to#86351
On 25.02.2015 00:05, baykiwi@gmail.com wrote:
>>>> 'http://xthunder'.strip('http://')
> 'xthunder'
>>>> 'http://thunder'.strip('http://')
> 'under'
>>>>
>
> I could understand backslash but forward slash?
>

You are misunderstanding what the argument to strip does. Look at this:

 >>> 'http://xthunder'.strip('/hpt:')
'xthunder'

[toc] | [prev] | [next] | [standalone]


#86358

FromChris Kaynor <ckaynor@zindagigames.com>
Date2015-02-24 15:22 -0800
Message-ID<mailman.19155.1424820483.18130.python-list@python.org>
In reply to#86351

[Multipart message — attachments visible in raw view] — view raw

On Tue, Feb 24, 2015 at 3:05 PM, <baykiwi@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.

[toc] | [prev] | [next] | [standalone]


#86394

FromRustom Mody <rustompmody@gmail.com>
Date2015-02-25 03:03 -0800
Message-ID<26f2f167-eead-4966-b567-f18eb4975bd2@googlegroups.com>
In reply to#86351
On Wednesday, February 25, 2015 at 4:35:37 AM UTC+5:30, bay...@gmail.com wrote:
> >>> 'http://xthunder'.strip('http://')
> 'xthunder'
> >>> 'http://thunder'.strip('http://')
> 'under'
> >>>
> 
> I could understand backslash but forward slash?

Others have answered specifically.
However you probably want to look at urlparse
https://docs.python.org/2/library/urlparse.html

[toc] | [prev] | [next] | [standalone]


#86569

FrombabyG <bintadoma20@yahoo.com>
Date2015-02-27 04:18 -0600
Message-ID<T-KdndGXqfjt2W3JnZ2dnUU7-eudnZ2d@giganews.com>
In reply to#86351
Hello how are you doing please

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web