-1

My understanding is that the lstrip(arg) removes characters from the left based on the value of arg.

I am executing the following code:

'htp://www.abc.com'.lstrip('/')

Output:

'htp://www.abc.com'

My understanding is that all the characters should be stripped from left until / is reached. In other words, the output should be:

'www.abc.com'

I am also not sure why running the following code is generating below output:

'htp://www.abc.com'.lstrip('/:pth')

Output:

'www.abc.com'
meallhour
  • 13,921
  • 21
  • 60
  • 117
  • lstip() strips all the specified characters from the left. try with `//////htp://www.abc.com` see [doc](https://docs.python.org/3/library/stdtypes.html#str.lstrip) – Tranbi Oct 21 '21 at 06:53

4 Answers4

2

Calling the help function shows the following:

Help on built-in function lstrip:

lstrip(chars=None, /) method of builtins.str instance
    Return a copy of the string with leading whitespace removed.
    
    If chars is given and not None, remove characters in chars instead.

Which, clearly means that any white-space in the starting (i.e. left) will be chopped-off or if the chars argument is specified it will remove those characters if and only if the string begins with any of the specified characters i.e. if you pass 'abc' as an argument then the string should start with any of 'a','b' or 'c' else the function won't change anything. The string need not begin with the 'abc' as a whole.

print(' the left strip'.lstrip())    # strips off the whitespace
the left strip
>>> print('ththe left strip'.lstrip('th'))    # strips off the given characters as the string starts with those
e left strip
>>> print('ththe left strip'.lstrip('left'))    # removes 't' as 'left' contatins 't' in it
hthe left strip
>>> print('ththe left strip'.lstrip('zeb'))    # doesn't change anything as the argument passed doesn't match the beggining of the string
ththe left strip
>>> print('ththe left strip'.lstrip('h'))    # doesn't change anything as the argument passed doesn't match the beggining of the string
ththe left strip
Ajay Singh Rana
  • 573
  • 4
  • 19
1

If you want all chars right of a given string try split

url = 'htp://www.abc.com'
print(url.split('//')[1])

output

www.abc.com

lstrip only returns a copy of the string with leading characters stripped, not in between

Shreyas Prakash
  • 604
  • 4
  • 11
  • can you please help me understand output of `'htp://www.abc.com'.lstrip('/:pth')`? I have just now updated my question – meallhour Oct 21 '21 at 06:59
  • 1
    @meallhour `strip()` treats `'/:pth'` as a *set* of characters to be stripped, not a *prefix* to be removed. So the order of the characters has no visible effect. – BoarGules Oct 21 '21 at 07:05
1

I think you want this :

a = 'htp://www.abc.com'
a = a[a.find('/')+1:]

From Python Docs : str.lstrip([chars])

Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. **The chars argument is not a prefix; rather, all combinations of its values are stripped:**

Read the last line your doubt will be resolved.

Deepak Tripathi
  • 3,175
  • 1
  • 8
  • 21
1

In the Python documentation, str.lstrip can only remove the leading characters specified in its args, or whitespaces if no characters is provided.

You can try using str.rfind like this:

>>> url = "https://www.google.com"
>>> url[url.rfind('/')+1:]
'www.google.com'
hung3a8
  • 21
  • 1