Basics
Bytes and Bytearray
Encode text when an external boundary needs bytes. UTF-8 uses one byte for ASCII characters and more than one byte for many other characters.
Source
text = "café"
data = text.encode("utf-8")
print(data)
print(len(text), len(data))Output
b'caf\xc3\xa9'
4 5Decode bytes when the program needs text again. The decoder must match the encoding used at the boundary.
Source
print(data.decode("utf-8"))Output
caféIndexing a bytes object returns an integer byte value, not a one-character bytes object.
Source
print(data[0])Output
99bytes is immutable. Use bytearray when binary data must be changed in place.
Source
packet = bytearray(b"py")
packet[0] = ord("P")
print(packet)Output
bytearray(b'Py')Notes
- Encode text when an external boundary needs bytes.
- Decode bytes when you want text again.
- Indexing
bytesreturns integers from 0 to 255. - Use
bytearraywhen binary data must be changed in place.
See also
- prerequisite: Strings
- related: Literals
- next depth: Networking
Run the complete example
Expected output
b'caf\xc3\xa9'
4 5
café
99
bytearray(b'Py')
Execution time appears here after you run the example.