bytes.translate() returns a new bytes object where each byte value b is replaced by table[b]. The table must be a bytes-like object of exactly 256 bytes, usually made with maketrans().
Pass None as the table to leave bytes unchanged, and use delete to remove every byte that appears in it. Deleting happens first, then mapping. It is the fastest way to apply a one-byte-to-one-byte mapping across a large buffer.
bytes.translate() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.translate(...). No import is needed, and bytearray objects have the same method.
Syntax
Give a table (or None) and, optionally, bytes to delete.
python
result = data.translate(table)
result = data.translate(table, delete)
result = data.translate(None, delete=b"\r")
Call
What it does
b"a-b".translate(None, b"-")
Returns b'ab'.
b"abc".translate(bytes.maketrans(b"a", b"A"))
Returns b'Abc'.
b"x".translate(bytes(256))
Returns b'\x00': every byte maps to zero.
The delete keyword name works on Python 3.6+; it can also be passed by position.
Method signatures
python
bytes.translate(table, /, delete=b'')
# table: bytes-like object of length 256, or None
# delete: bytes-like object listing bytes to remove
Parameters
Parameter
Description
table
A 256-byte mapping where position b holds the replacement for byte b, or None for no mapping.
delete
Optional. Every byte value that appears here is removed from the result.
Return Value
Return Value
Description
bytes
A new object with deletions applied and the remaining bytes mapped through the table.
Python Examples
Example 1 — Remove Separators with table=None
Clean up a phone-style number received as bytes by deleting dashes, spaces, and tabs, then delete all digits instead.
python
raw = b"12-34 56\t78"
print(raw.translate(None, b"- \t"))
print(raw.translate(None, delete=b"0123456789"))
Output
b'12345678'
b'- \t'
How It Works
No mapping.None means every kept byte stays as it is.
A set of bytes. The delete argument lists three separate bytes; each occurrence of any of them is dropped.
Keyword form. Deleting all ten digits leaves only the separators, and the tab prints as \t.
Example 2 — Build a ROT13 Table with maketrans()
ROT13 shifts each letter 13 places. Build the 256-byte table once, then use it to encode and decode.
Rotated alphabets. Slicing at 13 builds the target alphabet n-z followed by a-m, for both cases.
One table.maketrans() maps the 52 letters and leaves the other 204 byte values unchanged.
Self-inverse. Shifting by 13 twice covers all 26 letters, so the same table decodes the message.
Example 3 — Make Binary Data Safe to Display
Hex dump tools show unprintable bytes as dots. Build a table that keeps printable ASCII and maps everything else to ..
python
table = bytes(b if 32 <= b < 127 else ord(".") for b in range(256))
chunk = b"GET\r\n\x00\x01ok\xff"
print(len(table))
print(chunk.translate(table))
print(chunk.translate(table, b"\x00"))
Output
256
b'GET....ok.'
b'GET...ok.'
How It Works
Generated table. A generator expression yields one value for each of the 256 possible bytes.
Dots for the rest. CR, LF, the two control bytes, and \xff all become ..
Delete before mapping. The zero byte is removed first, so the result has one dot fewer.
Frequently Asked Questions
A bytes-like object of exactly 256 bytes, where table[b] is the replacement for byte b. bytes.maketrans() builds one for you.
Pass None as the table: data.translate(None, b"\r\n").
Deletion. Bytes listed in delete are removed, and the remaining bytes are then mapped through the table.
No. bytes.translate() needs a 256-byte table. A dict is for str.translate().
No. It is strictly one byte to one byte. Use replace() for longer replacements.