classification
Title: Add a parameter to strip, lstrip, and rstrip that treats the first parameter as a full string
Type: enhancement Stage:
Components: Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Chris Rogers, steven.daprano
Priority: normal Keywords:

Created on 2020-02-11 09:40 by Chris Rogers, last changed 2020-02-11 12:32 by steven.daprano.

Messages (2)
msg361791 - (view) Author: Chris Rogers (Chris Rogers) Date: 2020-02-11 09:40
Consider this string:

'mailto:mailto:mailto:main@example.com'

If you try to remove the mailtos with lstrip('mailto:'), you'll be left with this:

'n@example.com'

That's because the three strip functions look at each character separately rather than as a whole string.

Currently, as a workaround, you have to either use regex or a loop. This can take several lines of code.

It would be great if the strip functions had a second parameter that lets you keep the first parameter intact.

You could then use this code to get the desired result:

'mailto:mailto:mailto:main@example.com'.lstrip('mailto:', true)
>>main@example.com
msg361809 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-02-11 12:32
This has been discussed not long ago, it seems like it will need a PEP. See Brett's comment:

https://mail.python.org/archives/list/python-dev@python.org/message/BDK6BDBOG2462SJIIOC5QMYPAJ5A4523/

and various discussions:

https://mail.python.org/archives/list/python-dev@python.org/message/ZWRGCGANHGVDPP44VQKRIYOYX7LNVDVG/

https://mail.python.org/archives/list/python-ideas@python.org/thread/RJARZSUKCXRJIP42Z2YBBAEN5XA7KEC3/#WIRID57ESUFUAQQQ6ZUY2RK5PKQQYSJ3
History
Date User Action Args
2020-02-11 12:32:18steven.dapranosetnosy: + steven.daprano
messages: + msg361809
2020-02-11 09:40:20Chris Rogerscreate