Normalize Canonically Equivalent Text
Core idea
Unicode normalization produces a consistent representation of equivalent sequences. NFC composes canonical sequences where possible; NFD decomposes them. NFKC and NFKD additionally fold compatibility distinctions and can change meaningful presentation or identity.
import unicodedata as uda, b = "é", "e\u0301"print(a == b)print(ud.normalize("NFC", a) == ud.normalize("NFC", b))Raw equality compares code point sequences; NFC gives these spellings the same representation.
Observe compatibility folding
import unicodedata as uds = "①"print(ud.normalize("NFC", s) == s)print(ud.normalize("NFKC", s))Compatibility normalization removes the circled presentation here.
Common mistake
Normalization is not transliteration, case folding, or a complete visual-spoofing defense. Keep original identifiers and document a comparison policy.
Practice
Return whether two strings are canonically equivalent after NFC, without compatibility folding.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Unicode normalization
Check your understanding of unicode normalization.
What is printed?
import unicodedata as uprint(len(u.normalize("NFC", "a\u0308")))What is printed?
import unicodedata as uprint(len(u.normalize("NFD", "é")))Which forms apply compatibility mappings?
Why retain original text?