Unicode assigns code points to characters used in writing systems around the world.
How It Works
Python str values store text, while bytes represent encoded data. UTF-8 is a common encoding that turns Unicode text into bytes. ord() and chr() help inspect Unicode code points.
Example
letter = "é"
print(ord(letter))
print(chr(233))
print(letter.encode("utf-8"))
The outputs show code point 233, the character é, and its UTF-8 byte sequence.
Expected Output
233
é
b'\xc3\xa9'
Important Notes
A displayed character can consist of multiple Unicode code points; len(text) does not always equal visible glyph count.
Try It Yourself
Compare the character counts of two visually similar accent spellings.
Common Beginner Mistakes
- A displayed character can consist of multiple Unicode code points; len(text) does not always equal visible glyph count.
- Counting Unicode code points and assuming that always matches a user's visual character count.
- Attempting to decode an arbitrary byte sequence without knowing its character encoding.
Key Takeaways
- Unicode assigns code points to characters used in writing systems around the world.
- Python str values store text, while bytes represent encoded data.
- Unicode text and encoded bytes are different concepts; identify the encoding at boundaries.