DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

maketrans in Python

Buy Me a Coffee

*Memos:

str.maketrans() and bytes.maketrans() or bytearray.maketrans() can make the table to translate a string and byte string respectively with str.translate() and bytes.translate() or bytearray.translate() respectively as shown below:

*Memos for str.maketrans():

  • The 1st argument is x(Required-Type:dict{str/int:str/int/None} or str):
    • It must be dict if only one argument is set, which is recommended:
      • A key is replaced with a value.
      • str keys must be the length 1.
      • str keys are converted to Unicode numbers.
      • An empty string or None means nothing.
      • It can be an empty dictionary.
    • It must be str if two or three arguments are set. *It's the zero or more characters replaced with y.
    • Don't use x=.
  • The 2nd argument is y(Optional or Required-Type:str):
    • It's the zero or more characters replacing x.
    • It mustn't be set if x is dict.
    • It must be set if x is str.
    • Don't use y=.
  • The 3rd argument is z(Optional-Type:str):
    • It's the zero or more characters to delete.
    • Don't use z=.
  • The lengths of x and y must be the same.
  • The one or more characters of z is prioritized for deletion rather than replacement.

*Memos for bytes.maketrans() and bytearray.maketrans():

  • The 1st argument is from(Required-Type:bytes-like object):
    • It's the zero or more characters replaced with to.
    • Don't use from=.
  • The 2nd argument is to(Required-Type:bytes-like object):
    • It's the zero or more characters replacing from.
    • Don't use to=.
  • The lengths of from and to must be the same.

<String>:

<maketrans() with one argument>:

table1 = str.maketrans({
    'a': 'x',
    'b': 'y',
    'c': 'z',
    '1':'one',
    '2':'two',
    '3':'three',
    ' ': '    ',
    'd': '',
    'e': '',
    'f': '',
    'g': '',
    'h': ''
})

# The below is equivalent to the above.
table2 = str.maketrans({
    97: 120,     # 97('a') & 120('x')
    98: 121,     # 98('b') & 121('y')
    99: 122,     # 99('c') & 122('z')
    49: 'one',   # 49('1')
    50: 'two',   # 50('2')
    51: 'three', # 51('3')
    32: '    ',  # 32(' ')
    100: None,   # 100('d')
    101: None,   # 101('e')
    102: None,   # 102('f')
    103: None,   # 103('g')
    104: None    # 104('h')
})

print(table1)
# {97: 'x', 98: 'y', 99: 'z', 49: 'one', 50: 'two', 51: 'three',
#  32: '    ', 100: '', 101: '', 102: '', 103: '', 104: ''}

print(table2)
# {97: 120, 98: 121, 99: 122, 49: 'one', 50: 'two', 51: 'three',
#  32: '    ', 100: None, 101: None, 102: None, 103: None, 104: None}
Enter fullscreen mode Exit fullscreen mode
table = str.maketrans({
 'abc': '',
    '': 'abc'
})
# ValueError: string keys in translate table must be of length 1
Enter fullscreen mode Exit fullscreen mode

<maketrans() with two arguments>:

table1 = str.maketrans('abc', 'xyz')
table2 = str.maketrans('', '')

print(table1)
# {97: 120, 98: 121, 99: 122}

print(table2)
# {}
Enter fullscreen mode Exit fullscreen mode
table1 = str.maketrans('abc', 'x')
table2 = str.maketrans('x', 'abc')
# ValueError: the first two maketrans arguments must have equal length
Enter fullscreen mode Exit fullscreen mode

<maketrans() with three arguments>:

table1 = str.maketrans('abc', 'xyz', 'defgh')
table2 = str.maketrans('', '', '')

print(table1)
# {97: 120, 98: 121, 99: 122, 100: None,
#  101: None, 102: None, 103: None, 104: None}

print(table2)
# {}
Enter fullscreen mode Exit fullscreen mode
table1 = str.maketrans('abc', 'x', 'defgh')
table2 = str.maketrans('x', 'abc', 'defgh')
# ValueError: the first two maketrans arguments must have equal length
Enter fullscreen mode Exit fullscreen mode

<Byte String(UTF-8)>:

bytes() & bytearray():

table1 = bytes.maketrans('abc'.encode(), 'xyz'.encode())
table1 = bytes.maketrans(b'abc', b'xyz')
table1 = bytes.maketrans(bytearray('abc'.encode()), bytearray('xyz'.encode()))
table1 = bytearray.maketrans('abc'.encode(), 'xyz'.encode())
table1 = bytearray.maketrans(b'abc', b'xyz')
table1 = bytearray.maketrans(bytearray(b'abc'), bytearray(b'xyz'))
table2 = bytes.maketrans(''.encode(), ''.encode())
table2 = bytes.maketrans(b'', b'')
table2 = bytes.maketrans(bytearray(''.encode()), bytearray(''.encode()))
table2 = bytearray.maketrans(''.encode(), ''.encode())
table2 = bytearray.maketrans(b'', b'')
table2 = bytearray.maketrans(bytearray(''.encode()), bytearray(''.encode()))

print(table1)
# b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11
# \x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*
# +,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`xyzdefghijklm
# nopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b
# \x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c
# \x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad
# \xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe
# \xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf
# \xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0
# \xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1
# \xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'

print(table2)
# b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11
# \x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*
# +,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm
# nopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b
# \x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c
# \x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad
# \xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe
# \xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf
# \xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0
# \xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1
# \xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'
Enter fullscreen mode Exit fullscreen mode
table1 = bytes.maketrans('abc'.encode(), 'x'.encode())
table1 = bytes.maketrans(b'abc', b'x')
table1 = bytes.maketrans(bytearray('abc'.encode()), bytearray('x'.encode()))
table1 = bytearray.maketrans('abc'.encode(), 'x'.encode())
table1 = bytearray.maketrans(b'abc', b'x')
table1 = bytearray.maketrans(bytearray(b'abc'), bytearray(b'x'))
table2 = bytes.maketrans('x'.encode(), 'abc'.encode())
table2 = bytes.maketrans(b'x', b'abc')
table2 = bytes.maketrans(bytearray('x'.encode()), bytearray('abc'.encode()))
table2 = bytearray.maketrans('x'.encode(), 'abc'.encode())
table2 = bytearray.maketrans(b'x', b'abc')
table2 = bytearray.maketrans(bytearray(b'x'), bytearray(b'abc'))
# ValueError: maketrans arguments must have same length
Enter fullscreen mode Exit fullscreen mode

Top comments (0)