strip bug?
>>> 'http://xthunder'.strip('http://') 'xthunder' >>> 'http://thunder'.strip('http://') 'under' >>> I could understand backslash but forward slash? -- https://mail.python.org/mailman/listinfo/python-list
Re: strip bug?
On Tue, Feb 24, 2015 at 4:05 PM, 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. -- https://mail.python.org/mailman/listinfo/python-list
Re: strip bug?
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. -- https://mail.python.org/mailman/listinfo/python-list
Re: strip bug?
On 25-2-2015 0:05, bayk...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: strip bug?
On Tue, Feb 24, 2015 at 3:05 PM, 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. -- https://mail.python.org/mailman/listinfo/python-list
Re: strip bug?
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: strip bug?
Hello how are you doing please -- https://mail.python.org/mailman/listinfo/python-list