What does “table” in the string.translate function mean?

It depends on Python version you are using.

In Python 2.x. The table is 256-characters string. It can be created using string.maketrans:

>>> import string
>>> tbl = string.maketrans('ac', 'dx')
>>> "abcabc".translate(tbl)
'dbxdbx'

In Python 3.x, the table is mapping of unicode ordinals to unicode characters.

>>> "abcabc".translate({ord('a'): 'd', ord('c'): 'x'})
'dbxdbx'

Leave a Comment