Distinguish Code Points from Grapheme Clusters
Core idea
A Python str is a sequence of Unicode code points. A grapheme cluster is a user-perceived text unit that may contain several code points; len counts code points, not clusters or display columns.
a = "é"b = "e\u0301"print(len(a), len(b))print([hex(ord(c)) for c in b])The second spelling has a base letter followed by a combining accent. ord returns a code point number; hex displays it in hexadecimal.
Inspect an emoji sequence
s = "👩\u200d💻"print(len(s))print(ord(s[1]) == 0x200d)The joiner connects emoji components in a sequence commonly displayed as one cluster. Display depends on the renderer.
Common mistake
Slicing by code points can break a cluster. Use a grapheme-aware segmentation implementation when limiting user-visible characters; Python 3.13 has no built-in grapheme iterator.
Practice
Compare len and the code points of "آ" and "ا\u0653"; explain why appearance is not a reliable length measure.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Code points and visible characters
Check your understanding of code points and graphemes.
What is printed?
print(len("a\u0308"))What is printed?
print(ord("A"))What does a Python string index select?
Which approach should limit user-perceived characters?