0

Here is the sample code:

test = '0x0'
test.lstrip('0x')

I expect '0' but I see ''.

Is this some bug or expected behavior? If the latter, what is the best solution to obtain expected output?

Chris
  • 26,361
  • 5
  • 21
  • 42
kaytu
  • 17
  • 4

1 Answers1

1

The misunderstanding is thinking that str.lstrip strips that exact literal string from the left side of a string. Rather it strips each of the characters in that string.

>>> 'foo bar'.lstrip('ofb ')
'ar'

If you need to remove an exact substring from the beginning of a string, you may wish to use an approach like the following.

>>> s = 'foo bar'
>>> if s.startswith('fo'): s = s[len('fo'):]
... 
>>> s
'o bar'
Chris
  • 26,361
  • 5
  • 21
  • 42