Sub topic 2
Code Points, Encodings, and What a Character Is
Assume you know basic Python strings and have seen text stored as bytes in an encoding such as UTF-8. This chapter builds the model needed to clean text safely. The main idea is that a visible character, a Unicode code point, and a byte sequence are related, but they are not the same thing.
Three layers of text
A user-perceived character is one unit that a reader sees as a character. For example, é usually appears as one character. However, it can be represented by one code point or by multiple code points. An emoji such as a family can also contain several code points while appearing as one symbol.
A Unicode code point is a number assigned to a Unicode character or symbol. Unicode is a standard that gives text characters stable numeric identities. The letter A has code point U+0041. The character é can use U+00E9.
An encoding converts code points into bytes for storage or transmission. UTF-8 is a common encoding. It uses one byte for many basic Latin characters and multiple bytes for other characters. The bytes are not the characters themselves.
Why strings that look equal can differ
Two strings can render identically but contain different code point sequences. The first version of é below uses one code point. The second uses the letter e followed by a combining acute accent. A combining character modifies the character before it.
import unicodedata
single = "caf\u00e9"
combined = "cafe\u0301"
print(single) # café
print(combined) # café
print(single == combined) # False
print([f"U+{ord(ch):04X}" for ch in single])
print([f"U+{ord(ch):04X}" for ch in combined])
print(unicodedata.name(single[-1]))
print(unicodedata.name(combined[-1]))The strings look the same when printed, but equality compares their code point sequences. The first string ends with U+00E9, named LATIN SMALL LETTER E WITH ACUTE. The second ends with U+0301, named COMBINING ACUTE ACCENT.
The len() function also counts code points, not user-perceived characters:
print(len(single)) # 4
print(len(combined)) # 5This distinction matters for length limits, duplicate detection, search, and database keys. A visual comparison is not enough to decide whether two strings are equivalent for your task.
Encoding and decoding
Python stores a str as Unicode text. When you need bytes, encode the string. When you receive bytes, decode them using the encoding that was used to create them.
text = "café"
data = text.encode("utf-8")
print(data) # b'caf\xc3\xa9'
print(len(text)) # 4 code points
print(len(data)) # 5 bytes
restored = data.decode("utf-8")
print(restored == text) # TrueUTF-8 uses two bytes for é, even though Python reports it as one code point in this string. If you decode UTF-8 bytes as a different encoding, you may get incorrect text or an error. The bytes are not self-describing, so an application must know, or reliably determine, the encoding.
Code points and user-perceived characters
A code point is not always a complete visible character. Combining marks, skin-tone modifiers, joined emoji, and flags can use several code points. This means that slicing a string by index can split what a user sees as one unit.
For example, removing the last code point from a decomposed é removes the accent but leaves e. Removing one code point from some emoji sequences can leave an incomplete or unexpected symbol. If an application needs to count or slice user-perceived characters, use a grapheme-cluster-aware library rather than assuming that len(text) gives the visual character count.
Practical checks
When text behaves unexpectedly, inspect it instead of relying on its appearance:
- Print
repr(text)to reveal escape sequences and surrounding whitespace. - Print code points with
ord()to compare the underlying sequence. - Inspect
text.encode("utf-8")when investigating byte-level problems. - Record the expected encoding at file, API, and database boundaries.
- Normalize equivalent Unicode sequences before comparing them when the task allows it.
Unicode normalization can make canonically equivalent sequences consistent, but it is a separate transformation from encoding. Encoding changes between code points and bytes. Normalization changes the code point sequence itself.
Exercise
Why can "é" and "e\u0301" look identical but compare as unequal in Python?
Show answer
They contain different code point sequences. The first uses one precomposed code point, while the second uses e followed by a combining acute accent. Python compares the sequences, not their rendered appearance.