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

Ben Finney ben+python at benfinney.id.au
Thu Apr 12 03:47:07 CEST 2012

Tshepang Lekhonkhobe <tshepang at gmail.com>

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

How about:

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

You can't replace multiple matches “at the same time”, as you're
implying. The order of replacements is important, since it will affect
the outcome in many cases.

Do you think it's important to allow a set as the first argument to

    search_strings = set(['foo', 'bar'])
    'foo bar baz'.replace(search_strings, 'foobar')

I think that would be at least as desirable as your proposal; but what
would be the order of replacements?

 \      “Shepherds … look after their sheep so they can, first, fleece |
  `\   them and second, turn them into meat. That's much more like the |
_o__)      priesthood as I know it.” —Christopher Hitchens, 2008-10-29 |
Ben Finney

More information about the Python-ideas mailing list