August 2018
Intermediate to advanced
366 pages
10h 14m
English
The core of this recipe is the usage of translation tables. Translation tables are mappings that link a character to its replacement. A translation table like {'c': 'A'} means that any 'c' must be replaced with an 'A'.
str.maketrans is the function used to build translation tables. Each character in the first argument will be mapped to the character in the same position in the second argument. Then all characters in the last argument will be mapped to None:
>>> str.maketrans('a', 'b', 'c')
{97: 98, 99: None}The 97, 98, and 99 are the Unicode values for 'a', 'b', and 'c':
>>> print(ord('a'), ord('b'), ord('c'))
97 98 99Then our mapping can be passed to str.translate to apply it on the target string. The interesting ...