Python bytes.translate() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
bytes method

Definition

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")
CallWhat 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

ParameterDescription
tableA 256-byte mapping where position b holds the replacement for byte b, or None for no mapping.
deleteOptional. Every byte value that appears here is removed from the result.

Return Value

Return ValueDescription
bytesA 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"))

How It Works

  1. No mapping. None means every kept byte stays as it is.
  2. A set of bytes. The delete argument lists three separate bytes; each occurrence of any of them is dropped.
  3. 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.

python
lower = b"abcdefghijklmnopqrstuvwxyz"
upper = lower.upper()
table = bytes.maketrans(
    lower + upper,
    lower[13:] + lower[:13] + upper[13:] + upper[:13],
)

secret = b"Hello, World!".translate(table)
print(secret)
print(secret.translate(table))

How It Works

  1. Rotated alphabets. Slicing at 13 builds the target alphabet n-z followed by a-m, for both cases.
  2. One table. maketrans() maps the 52 letters and leaves the other 204 byte values unchanged.
  3. 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"))

How It Works

  1. Generated table. A generator expression yields one value for each of the 256 possible bytes.
  2. Dots for the rest. CR, LF, the two control bytes, and \xff all become ..
  3. 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.

Next: bytes.upper()

Return a copy with all ASCII lowercase letters converted to uppercase.

upper() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful