Python 3.1+Byte sequencesfunction
bytearray.maketrans
bytearray — replace and translate
A static method building a translation table.
Syntax
ba.maketrans(...)
It is a static method, so call it on the type (bytearray.maketrans) rather than on an instance; the result is a plain bytes object of length 256 whose i-th byte is the replacement for byte value i. Both arguments must have the same length or you get a ValueError, and mapping is strictly one byte to one byte — you cannot expand a byte into two. Deletion is not part of the table: pass the bytes to drop as translate's second argument.
Examples
print(len(bytearray.maketrans(b'ab', b'AB'))) # → 256
table = bytearray.maketrans(b'ab', b'AB')
print(bytearray(b'cabbage').translate(table)) # → bytearray(b'cABBAge')
print(bytearray(b'ATGCA').translate(bytearray.maketrans(b'ATGC', b'TACG'))) # → bytearray(b'TACGT')
print(type(bytearray.maketrans(b'ab', b'AB'))) # → <class 'bytes'>
print(bytearray.maketrans(b'ab', b'A')) # → ValueError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy