php has the strtr function:
strtr(\'aa-bb-cc\', array(\'aa\' => \'bbz\', \'bb\' => \'x\', \'cc\' => \'y\'));
# bbz-x-y
It replace
The following uses regular expressions to do it:
import re
def strtr(s, repl):
pattern = '|'.join(map(re.escape, sorted(repl, key=len, reverse=True)))
return re.sub(pattern, lambda m: repl[m.group()], s)
print(strtr('aa-bb-cc', {'aa': 'bbz', 'bb': 'x', 'cc': 'y'}))
Like the PHP's version, this gives preference to longer matches.