Issue 33362: string strip() strips extra characters that it shouldn't
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) * ![]() |
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:59 | admin | set | github: 77543 |
| 2018-04-26 04:55:42 | davidghiurco | set | messages: + msg315774 |
| 2018-04-26 04:09:16 | steven.daprano | set | status: open -> closed nosy:
+ steven.daprano resolution: not a bug |
| 2018-04-26 02:55:51 | pmpp | set | nosy:
+ pmpp messages: + msg315772 |
| 2018-04-26 02:50:46 | davidghiurco | create | |
