I ran into what I think is a bug, and I'm looking for confirmation or that I am not understanding how this method works.
Here's my basic output:
(Pdb) x = 'KEY_K'
(Pdb) x.lstrip('K')
'EY_K'
(Pdb) x.lstrip('KE')
'Y_K'
(Pdb) x.lstrip('KEY')
'_K'
(Pdb) x.lstrip('KEY_')
''
(Pdb) import sys
(Pdb) sys.version
'2.7.11 (default, Dec 5 2015, 14:44:47) \n[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)]'
My understanding is that the final 'lstrip' in that example should have returned 'K', but it did not. Does anyone know why?
The second arg of lstrip is the set of characters that will be removed. If you need to remove a substring, you may use:
if x.startswith('KEY_'):
x = x.replace('KEY_', '', 1)
The lstrip()
function doesn't behave in exactly the way you think it might. x.lstrip(argument)
removes any of the characters in argument
from the left of the string x
until it reaches a character not in argument
.
So 'KEY_K'.lstrip('KEY_')
generates ''
because the last character, K
, is in KEY_
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With