In Python, the string type (str) handles Unicode characters. However, when dealing with image files, audio data, or network communication packets, you need raw binary data.
The bytes type is used to represent this binary data. The bytes type is a sequence of integers from 0 to 255 (representing 1 byte each). It has the property of being immutable, meaning it cannot be changed once created.
This article explains how to create bytes objects and their basic characteristics.
1. Creating from a List of Integers
You can create a bytes sequence by passing a list of integers to the bytes() constructor. Each integer in the list must be within the range representable by 1 byte, which is 0 to 255.
# List of integers from 0 to 255
# For example, assuming a simple binary pattern or color codes
binary_values = [10, 20, 128, 255]
# Convert list to bytes type
data_bytes = bytes(binary_values)
print(f"Value: {data_bytes}")
print(f"Type: {type(data_bytes)}")
Output:
Value: b'\n\x14\x80\xff'
Type: <class 'bytes'>
Understanding the Output:
b'...': Indicates that this is a bytes sequence.\n: If there is a corresponding ASCII character (10 is newline), that character or escape sequence is displayed.\x80: Values that do not correspond to ASCII characters are displayed in hexadecimal format following\x.
Using Out-of-Range Values
If the list contains numbers 256 or higher, or negative numbers, a ValueError will occur.
# List containing an out-of-range value (256)
invalid_values = [0, 100, 256]
# Trying to convert causes an error
# bytes(invalid_values)
# ValueError: bytes must be in range(0, 256)
2. Creating with Byte Literals (b"...")
You can create a bytes sequence directly by enclosing text in quotes and adding a b prefix, similar to strings. This is called a “Byte Literal”. However, you can only use ASCII characters (alphanumeric characters and some symbols) with this notation. Multi-byte characters (like Japanese) cannot be written directly inside b"...".
# Creating with a byte literal
header_signature = b"IMG_HEADER_v1"
print(f"Header Signature: {header_signature}")
# Accessing by index returns the corresponding integer value (character code)
print(f"1st char ('I') integer value: {header_signature[0]}")
print(f"2nd char ('M') integer value: {header_signature[1]}")
Output:
Header Signature: b'IMG_HEADER_v1'
1st char ('I') integer value: 73
2nd char ('M') integer value: 77
Characteristics of bytes: Immutable
Like strings and tuples, the bytes type is Immutable (unchangeable). You cannot rewrite a byte at a specific position after it has been created.
packet_data = b"DATA:1234"
# Writing by index causes an error
# packet_data[0] = 65
# TypeError: 'bytes' object does not support item assignment
If you need a modifiable sequence of bytes, you must use the bytearray type instead.
Summary
The bytes type is used to represent raw data (binary) handled by computers.
- Creation Methods:
bytes([10, 255, ...]): Convert from a list of integers (0-255).b"ASCII": Create directly from ASCII string (Byte Literal).
- Characteristics:
- Elements are managed as integers from 0 to 255.
- It is immutable; modification after creation is not allowed.
This knowledge is essential when performing low-level processing such as file manipulation or socket communication.
