Search notes:

Python: str.translate()

#!/usr/bin/python3

str = 'Foo bar baz';

#
# Replace o with u
#
print(str.translate( str.maketrans( 'o' , 'u'  ))) # Fuu bar baz

#
# Replace o with u, b with p
#
print(str.translate( str.maketrans( 'ob', 'up' ))) # Fuu par paz

#
# Following line causes: ValueError: the first two maketrans arguments must have equal length
#
# print(str.translate( str.maketrans( 'obr', 'up' )))
Github repository about-python, path: /built-in/types/str/translate.py

See also

str.replace()
str

Index