[Python-ideas] Easily remove characters from a string.

Chris Angelico rosuav at gmail.com
Mon Oct 24 17:56:07 EDT 2016


On Tue, Oct 25, 2016 at 4:48 AM, Chris Barker <chris.barker at noaa.gov> wrote:
> On Mon, Oct 24, 2016 at 8:21 AM, Michel Desmoulin
> <desmoulinmichel at gmail.com> wrote:
>>
>> This actually could be implemented directly in str.replace() without
>> breaking the API by accepting:
>>
>> "stuff".replace('a', '')
>> "stuff".replace(('a', 'b', 'c'), '')
>> "stuff".replace(('a', 'b', 'c'), ('?', '*', ''))
>
>
> +1 -- I have found I Need to do this often enough that I've wondered why
> it's not there.
>
> making three calls to replace() isn't too bad, but is klunky and has
> performance issues.

And it may not be semantically identical. In the examples above, three
separate replace calls would work, but a syntax like this ought to be
capable of an exchange - "aabbccdd".replace(('b', 'd'), ('d', 'b')) ==
"aaddccbb".

ChrisA


More information about the Python-ideas mailing list