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

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Apr 12 04:47:42 CEST 2012

Ben Finney wrote:
> Tshepang Lekhonkhobe <tshepang at gmail.com>
> writes:

>>>>>'foo bar baz'.replace(('foo', 'bar'), 'baz')

> You can't replace multiple matches “at the same time”, as you're
> implying.

An obvious thing to do is to try them in the order they
appear in the sequence. That would argue against allowing
an unordered collection.

Not quite so obvious is whether the replacements should
be considered as candidates for further replacements.
I would say not, because it complicates the algorithm
and in my experience is rarely needed. If you want that,
you would just have to do multiple replace calls like
you do now.

And how about allowing a sequence of (old, new) pairs
instead of just a single replacement? That would be even
more useful.


More information about the Python-ideas mailing list