A translation table maps characters to replacement values for str.translate(). str.maketrans() creates the table from a dictionary, while unmapped characters remain unchanged. section 4.4.3 section 8.1.2
A translation table supports these behaviors:
- Replace every occurrence of a mapped character, such as digits in a phone number. section 4.4.3
- Leave characters absent from the mapping untouched, preserving letters, spaces, and punctuation. section 8.1.2
- Represent mappings using Unicode ordinals, strings, or None; None deletes the character. section 8.1.2
Translate a phone number
jumper = {'1': '9', '2': '8', '3': '7', '4': '6', '5': '0', '6': '4', '7': '3', '8': '2', '9': '1', '0': '5'} text = 'Jenny = 867-5309' print(text.translate(str.maketrans(jumper))) # Jenny = 243-0751 section 4.4.3 section 8.1.2

