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

Sven Marnach sven at marnach.net
Thu Apr 12 18:20:13 CEST 2012


INADA Naoki schrieb am Thu, 12. Apr 2012, um 22:17:30 +0900:
> Oh, I didn't know that. Thank you.
> But what about unescape? str.translate accepts only one character key.

You'd currently need to use the `re` module:

    >>> d = {"&": "&", ">": ">", "<": "<"}
    >>> re.sub("|".join(d), lambda m: d[m.group()], "<>&")
    '<>&'

Cheers,
    Sven



More information about the Python-ideas mailing list