Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Class

bytes

bytes is Python’s immutable binary sequence type. It stores individual byte values as integers from 0 through 255 and can be created from literals, iterable data, or encoded text.

How it works

bytes creates an immutable binary sequence whose elements are integers from 0 through 255. Use a bytes literal or construct it from an iterable of integers, an existing bytes-like source, or text together with an encoding and optional error policy; convert it back to text with decode().

Syntax

bytes(source=b'')
bytes(source, encoding, errors='strict')

Parameters

NameTypeDescription
sourceobjectSource value, defaulting to empty bytes; it may provide the initial byte sequence.
encodingstrText encoding used when source is a string.
errorsstrPolicy for encoding errors, defaulting to strict.

Returns

A new immutable bytes object containing values in the range 0 through 255.

Key points

  • The constructor produces immutable binary data, and each element is an integer from 0 through 255.
  • bytes provides the non-mutating counterpart to bytearray; choose bytearray when in-place changes are required.
  • Its constructor accepts the same source forms as bytearray(), including an optional encoding and error policy for text.
  • For fixed binary content, use a b-prefixed literal instead of calling the constructor.
  • A bytes value is a sequence type, so iteration, indexing, slicing, and many sequence operations are available.
  • Several methods target ASCII-compatible binary protocols, making bytes useful for wire formats and encoded payloads.

Common mistakes

  • Bytes literals cannot contain non-ASCII characters directly; represent such values with escape sequences or encode a string.
  • Every element must be in the inclusive range 0 through 255; values outside that range raise ValueError.
  • Indexing produces an integer, not a one-byte bytes object; use slicing when a bytes result is needed.

Example: Encode, decode, and index bytes

payload = "café".encode("utf-8")
print(payload)
print(payload.decode("utf-8"))
print(payload[0])

The string is encoded into immutable bytes, then decoded back to text; indexing returns the integer value of one byte.

When to use it

Binary data

Representing network packets, file contents, or serialized payloads without accidental mutation.

Text encoding

Turning Unicode text into a protocol or storage representation with an explicit character encoding.

Protocol processing

Reading protocol fields whose values are ASCII-compatible byte sequences.

Interview questions

QWhat does a bytes object represent?

bytes is an immutable sequence of integers, with each integer constrained to 0 through 255. It is suited to binary data and supports sequence operations such as indexing and slicing.

Follow-up: How would you obtain text from it? Call decode() with the encoding used for the original text; the chosen error policy can also affect handling of invalid data.

QHow do you construct bytes from a string?

Use bytes(text, encoding, errors) or text.encode(encoding, errors) when starting from text. The encoding is required for text conversion, while the error handling argument defaults to strict behavior.

Follow-up: Why can bytes(text) not be used directly? A text object needs an explicit encoding to define how its characters become binary values.

QWhat restrictions apply to bytes literals?

A bytes literal uses a b prefix, such as b'abc', and supports string-like quoting forms. Direct literal characters must be ASCII; other byte values require escape sequences.

Follow-up: How can hexadecimal input be handled? Use the fromhex() class method to create bytes from a hexadecimal string.

  • bytearray
  • memoryview
  • bytes literals
  • Bytes and Bytearray Operations
  • decode()
  • fromhex()

Source: Built-in Types. Checked 2026-10-16.