How to explain the str.maketrans function in Python 3.6?

str.maketrans builds a translation table, which is a mapping of integers or characters to integers, strings, or None. Think of it like a dictionary where the keys represent characters in the input string and the values they map to represent characters in the output string.

We go through the string to translate and replace everything that appears as a key in the mapping with whatever its value in the map is, or remove it if that value is None.

You can build a translation table with one, two, or three arguments (I think this may be what's confusing you). With one argument:

str.maketrans({'a': 'b', 'c': None})

You give the function a mapping that follows the rules for translation tables and it returns an equivalent table for that mapping. Things that map to None are removed

With two arguments:

str.maketrans('abc', 'xyz')

You give it two strings. Each character in the first string is replaced by the character at that index in the second string. So 'a' maps to 'x', 'b' to 'y', and 'c' to 'z'.

The one you're using, with three arguments, works the same as two arguments, but has a third string.

str.maketrans('abc', 'xyz', 'hij')

This is the same as the two argument version, except that the characters from the third string are removed, as if they were mapped to None. So your table is saying "Don't replace anything, but remove the characters that show up in this string".


From the documentation on str.maketrans:

If there is a third argument, it must be a string, whose characters will be mapped to None in the result.

This is what str.maketrans is doing; it is taking each element in the third argument and creating a map (a Python dictionary) that maps each ordinal value of the characters in the string to None:

>>> str.maketrans('', '', '0123456789') 
{48: None,
 49: None,
 50: None,
 51: None,
 52: None,
 53: None,
 54: None,
 55: None,
 56: None,
 57: None}

If extra values exist as the first and second arguments, they are added to this mapping as additional characters to be translated (this is why the author selected '' and ''; he doesn't want extra characters to be translated):

>>> str.maketrans('a', 'A', '0123456789') 
{48: None,
 49: None,
 50: None,
 51: None,
 52: None,
 53: None,
 54: None,
 55: None,
 56: None,
 57: None,
 97: 65}   # map ord('a') to ord('A')

If you apply this to your string now, it'll also capitalize 'athens' to 'Athens' due to the extra 'a', 'A' we've provided to maketrans. Not the finest of translations but suffices to grasp the functionality.

str_obj.translate will then perform look-ups on this dictionary for every character in str_obj replacing its values with the ones found in the mapping. If it doesn't find it inside the mapping, it leaves it as-is, if it is None it removes it. This is stated in the documentation for str.translate:

When indexed by a Unicode ordinal (an integer), the table object can do any of the following: return a Unicode ordinal or a string, to map the character to one or more other characters; return None, to delete the character from the return string; or raise a LookupError exception, to map the character to itself.

(Emphasis mine)