๐Ÿ”กbytes, bytearray, and encode/decodeLESSON

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

EncodingDescriptionCharacters
UTF-8Variable-width, backward-compatible with ASCIIAll Unicode
UTF-162 or 4 bytes per characterAll Unicode
ASCII1 byte per character128 characters (English)
latin-11 byte per character256 characters (Western European)
cp1252Windows Western EuropeanSimilar 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?