[Python-ideas] in str.replace(old, new), allow 'old' to accept a tuple

Raymond Hettinger raymond.hettinger at gmail.com
Fri Apr 13 04:03:00 CEST 2012


On Apr 11, 2012, at 4:35 PM, Tshepang Lekhonkhobe wrote:

> I find the fact that 'prefix' in str.startswith(prefix) accept a tuple
> quite useful. That's because one can do a match on more than one
> pattern at a time, without ugliness. Would it be a good idea to do the
> same for str.replace(old, new)?
> 
> before
>>>> 'foo bar baz'.replace('foo', 'baz').replace('bar', 'baz')
> baz baz baz
> 
> after
>>>> 'foo bar baz'.replace(('foo', 'bar'), 'baz')
> baz baz baz

It seems to meet that it is a rare use case to want
to replace many things with a single replacement string.
I can't remember a single case of ever needing this.
This only thing that comes to mind is automated redaction.

What I have needed and have seen others need is a dictionary
based replace:     {'customer': 'client', 'headquarters': 'office', 'now': 'soon'}.
Even that case is a fraught with peril -- I would want "now" to change
to "soon" but not have "snow" change to "ssoon".

In the end, I think want people want is to have the power
and control afforded by re.sub() but without having
to learn regular expressions.


Raymond
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120412/288c5c84/attachment.html>


More information about the Python-ideas mailing list