Bytearray Methods

bytearray.maketrans()

Construct an immutable 256-byte lookup table for binary translate(). Each input byte at a given position is mapped to the destination byte at that position. Unspecified byte values map to themselves. Both arguments must be equally long bytes-like objects. Repeated source values use the last mapping on CPython 3.13.5. The result is bytes even when this static method is called on bytearray. Unlike str.maketrans(), this table cannot expand a byte into several bytes or represent deletion; use translate(delete=...) to remove bytes.

Formal Syntax & Structure

python syntaxpython
bytearray.maketrans(frm, to, /)

Parameters & Attributes

NameTypeDescription
frmbytes-likeSource byte values.
tobytes-likeDestination values of equal length.

Return Value

bytes

Immutable lookup table of exactly 256 entries.

Working Example

Example Snippetpython
table = bytearray.maketrans(b"ab", b"XY")print(len(table), type(table).__name__)print(table[97], table[99])print(bytearray(b"cab").translate(table))
Python Reference