This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: string strip() strips extra characters that it shouldn't
Type: Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: davidghiurco, pmpp, steven.daprano
Priority: normal Keywords:

Created on 2018-04-26 02:50 by davidghiurco, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg315771 - (view) Author: David Ghiurco (davidghiurco) Date: 2018-04-26 02:50
No PR is attached since the issue is pretty trivial to reproduce but important nonetheless:

string = "LDA/AIC2/1919uc1b354363457"
print(string)
print(string.lstrip("LDA/"))

the stripped string should be "AIC2/1919uc1b354363457"
but is instead "IC2/1919uc1b354363457"

notice the leading "A" missing.

I've noticed this happens when the letter immediately following the slash is the same letter as the one immediately before the slash. Never contributed to python so I'm not exactly sure how to, but if anyone knowledgeable could take a look, this will probably be an easy fix.

Note: I am experiencing this on Python 3.6.4, specifically the Anaconda distribution. I have not tried another version.
msg315772 - (view) Author: pmp-p (pmpp) * Date: 2018-04-26 02:55
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.


all L D A / single chars get removed, while there is *any* of them of the left.
not the "LDA/" block as you probably expected
msg315773 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2018-04-26 04:09
the argument to string.strip (also lstrip and rstrip) is not an exact substring to remove, but a set of characters to remove:

py> 'zyxzz1234'.strip('xyz')
'1234'
msg315774 - (view) Author: David Ghiurco (davidghiurco) Date: 2018-04-26 04:55
Thanks guys. I should have used .replace(). My bad.
History
Date User Action Args
2022-04-11 14:58:59adminsetgithub: 77543
2018-04-26 04:55:42davidghiurcosetmessages: + msg315774
2018-04-26 04:09:16steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg315773

resolution: not a bug
stage: resolved
2018-04-26 02:55:51pmppsetnosy: + pmpp
messages: + msg315772
2018-04-26 02:50:46davidghiurcocreate