Back
+15 XP
Concept

Character Encodings, UTF-8 vs ASCII

Convert human Unicode strings to wire bytes using `.encode('utf-8')`. Convert wire bytes back to human strings using `.decode('utf-8')`.

python
text = "Python 🐍"
wire_bytes = text.encode("utf-8")
print(wire_bytes)  # b'Python \xf0\x9f\x90\x8d'
restored = wire_bytes.decode("utf-8")
print(restored)    # Python 🐍

# 20. Character Encodings, UTF-8 vs ASCII Convert human Unicode strings to wire bytes using `.encode('utf-8')`. Convert wire bytes back to human strings using `.decode('utf-8')`: ```python text = "Python 🐍" wire_bytes = text.encode("utf-8") print(wire_bytes) # b'Python \xf0\x9f\x90\x8d' restored = wire_bytes.decode("utf-8") print(restored) # Python 🐍 ``` ASCII only supports 128 basic English characters (1 byte per char). UTF-8 is the universal internet standard: it uses 1 byte for English, and dynamically expands up to 4 bytes for emojis, Arabic, and Asian scripts.

Checkpoint

Which method converts a human Unicode `str` into physical `bytes`?

Practice

Encode `"Hello"` to bytes with `.encode("utf-8")` and print `len(bytes_obj)` to verify it occupies exactly 5 bytes.

Terminal
01
Tap code blocks below to assemble
Available Blocks