Post

Python bytes() Method

In this tutorial, we will understand about the python bytes() method and its uses.

Python bytes() Method

Python’s bytes() function creates an immutable sequence of bytes from a given source. It accepts up to three arguments: a source object (an integer, string, iterable, or no argument), an optional encoding (required when the source is a string), and an optional error-handling scheme. When passed an integer, it creates a zero-filled bytes object of that length. When passed a string with an encoding, it converts the string to its byte representation. When passed an iterable of integers (each in the range 0-255), it creates a bytes object from those values. When called with no arguments, it returns an empty bytes object. The returned bytes object is immutable — once created, its contents cannot be changed. If you need a mutable byte sequence, use Python bytearray() instead. The bytes() function is essential for working with binary data, network protocols, file I/O, cryptographic operations, and encoding text for transmission over the wire. It is a cornerstone of Python’s binary data handling alongside Python str() for text data.

What does bytes() return?

The bytes() function returns an immutable bytes object. The content depends on the input: zero-filled bytes for an integer, encoded bytes for a string, or a byte sequence from an iterable of integers.

When should you use bytes()?

Use bytes() when you need to convert data into a binary format for storage, transmission, or processing. Common scenarios include encoding strings for network communication, preparing data for cryptographic hashing, writing binary files, and working with APIs that expect raw byte data.

What is the Python bytes() Method?

The Python bytes() method converts a given object and returns an immutable byte representation of that object. Understanding bytes() is fundamental to any Python work involving binary data — from reading image files and sending network packets to implementing cryptographic algorithms and working with hardware interfaces.

In Python 3, there is a strict separation between text (the str type) and binary data (the bytes type). This separation prevents a whole class of encoding bugs that were common in Python 2. The bytes() function is the standard way to bridge the gap between text or numeric data and the raw binary world.

The syntax of the bytes() method is:

1
bytes(source, encoding, errors)

All three parameters are optional, but encoding is required whenever source is a string. If you need a mutable version, use the bytearray() method instead.

Python bytes() Parameters

bytes() takes three optional parameters:

  • source — The object to be converted. Can be an integer, string, iterable, buffer-compatible object, or omitted entirely.
  • encoding — A string naming the encoding to use when source is a string (e.g., 'utf-8', 'ascii', 'latin-1').
  • errors — How to handle encoding errors. Common values: 'strict' (raise an error, default), 'ignore' (skip bad bytes), 'replace' (substitute a placeholder).

The source parameter can initialize the bytes object in the following ways:

  • Integer — Returns a zero-filled bytes object of that length.
  • String — Returns the bytes encoding of the given string (requires encoding).
  • Iterable — Returns a bytes object whose values match the iterable elements (each must be 0–255).
  • Buffer-compatible object — Uses the buffer protocol to read raw bytes.
  • No Source — Returns an empty bytes object b''.

Examples of bytes()

Example 1: Converting an integer to bytes

Passing an integer creates a bytes object of that length, filled with zero bytes:

1
2
3
4
5
6
int_value = 4

result = bytes(int_value)

print(result)
print(len(result))

Output:

1
2
b'\x00\x00\x00\x00'
4

Each \x00 represents a single zero byte. This is useful for allocating fixed-size binary buffers.

Example 2: Converting a string to bytes

When the source is a string, you must provide an encoding:

1
2
3
4
5
6
my_string = "Python Scholar"

result = bytes(my_string, 'utf-8')

print(result)
print(type(result))

Output:

1
2
b'Python Scholar'
<class 'bytes'>

Example 3: Converting an iterable to bytes

Any iterable of integers (each in the range 0–255) can be converted:

1
2
3
4
5
my_list = [1, 2, 3, 4, 5]

result = bytes(my_list)

print(result)

Output:

1
b'\x01\x02\x03\x04\x05'

Values outside the range 0–255 will raise a ValueError.

Example 4: Encoding with non-ASCII characters

1
2
3
4
5
6
7
8
9
text = "Héllo"

utf8_bytes = bytes(text, 'utf-8')
latin1_bytes = bytes(text, 'latin-1')

print("UTF-8:", utf8_bytes)
print("Latin-1:", latin1_bytes)
print("UTF-8 length:", len(utf8_bytes))
print("Latin-1 length:", len(latin1_bytes))

Output:

1
2
3
4
UTF-8: b'H\xc3\xa9llo'
Latin-1: b'H\xe9llo'
UTF-8 length: 6
Latin-1 length: 5

UTF-8 uses two bytes for é, while Latin-1 uses one — the same string produces different byte sequences depending on encoding.

Example 5: Using the errors parameter

1
2
3
4
5
6
7
text = "Héllo"

result_ignore = bytes(text, 'ascii', errors='ignore')
result_replace = bytes(text, 'ascii', errors='replace')

print("Ignore:", result_ignore)
print("Replace:", result_replace)

Output:

1
2
Ignore: b'Hllo'
Replace: b'H?llo'

Real-World Use Cases

Network Communication

When sending data over TCP/UDP sockets, Python requires bytes — not strings:

1
2
3
4
message = "GET / HTTP/1.0\r\n\r\n"
raw = bytes(message, 'utf-8')
print(f"Sending {len(raw)} bytes")
# sock.send(raw)

Cryptographic Hashing

Hash functions in hashlib require byte input:

1
2
3
4
5
6
import hashlib

password = "my_secret_password"
password_bytes = bytes(password, 'utf-8')
digest = hashlib.sha256(password_bytes).hexdigest()
print("SHA-256:", digest)

Writing Binary Files

When writing binary file formats, work directly with bytes:

1
2
3
4
5
6
header = bytes([0x89, 0x50, 0x4E, 0x47])  # PNG magic bytes

with open("header.bin", "wb") as f:
    f.write(header)

print("Wrote", len(header), "bytes")

Edge Cases and Gotchas

  • bytes() vs .encode(): bytes(s, 'utf-8') and s.encode('utf-8') produce identical results. .encode() is generally preferred for readability on string objects.
  • Integer range: Every element in an iterable must be in the range [0, 255]. Out-of-range values raise ValueError.
  • Immutability: You cannot modify a bytes object after creation. Use bytearray() for mutable byte data.
  • Bytes literals: In Python source, you can write b"hello" directly — equivalent to bytes("hello", "ascii") for ASCII text.

Tips and Best Practices

  1. Prefer .encode() over bytes(s, encoding) for string-to-bytes conversions — it reads more naturally.
  2. Always specify encoding explicitly — use 'utf-8' unless you have a specific reason for another encoding.
  3. Use bytearray() when mutation is needed — building up a byte buffer incrementally is more efficient with bytearray.
  4. Validate iterable values before passing to bytes() — ensure all integers are in range 0–255.
  5. Use descriptive variable names like payload_bytes or encoded_data to signal that a variable holds binary data.

Frequently Asked Questions

Q1: What is the difference between bytes() and bytearray()?

Both create sequences of bytes, but bytes is immutable — you cannot change its contents after creation. bytearray is mutable, meaning you can modify individual bytes in place. Use bytes for fixed, read-only byte sequences and bytearray when you need to build or modify byte data incrementally.

Q2: Do I need to specify encoding when converting a list of integers?

No. Encoding is only required when the source is a string. When you pass a list of integers, bytes() uses those values directly as byte values. For example, bytes([65, 66, 67]) returns b'ABC' without any encoding argument.

Q3: What happens if I pass a value greater than 255 in the iterable?

Python raises ValueError: bytes must be in range(0, 256). Each element in the iterable must be a valid byte value between 0 and 255 inclusive. For multi-byte integers, use struct.pack() instead.

Khushal Jethava
Khushal Jethava

Machine Learning Engineer at Codiste, specializing in Generative AI, NLP, and Computer Vision. Building production AI systems with Python.

This post is licensed under CC BY 4.0 by the author.