๐กbytes, bytearray, and encode/decodeLESSON~12 min
bytes, bytearray, and encode/decode
While Python strings (str) deal with text (Unicode characters), bytes and bytearray deal with raw binary data โ sequences of integers from 0 to 255. Understanding the distinction is crucial for network programming, file I/O, and working with binary formats.
The str vs bytes Distinction
In Python 3, str and bytes are completely separate types:
Creating bytes
bytes Operations
bytearray โ Mutable bytes
bytearray is the mutable counterpart of bytes. Use it when you need to modify binary data in place:
Encoding: str โ bytes
To convert text to bytes, you must specify an encoding โ a mapping from characters to byte sequences. UTF-8 is the standard:
Decoding: bytes โ str
Common Encodings
Encoding
Description
Characters
UTF-8
Variable-width, backward-compatible with ASCII
All Unicode
UTF-16
2 or 4 bytes per character
All Unicode
ASCII
1 byte per character
128 characters (English)
latin-1
1 byte per character
256 characters (Western European)
cp1252
Windows Western European
Similar to latin-1
Always use UTF-8 unless you have a specific reason not to.
Practical Use Cases
Reading/Writing Binary Files
Network Data
Checking File Signatures (Magic Bytes)
Knowledge Check
What does b"Hello"[0] return in Python?
Which method converts a str to bytes?
What is the key difference between bytes and bytearray?