Sub topic 4
Unicode Normalisation: NFC and NFD
Prerequisites
This chapter assumes you know basic Python strings, functions, and list operations. You should also know that text may be stored as bytes in an encoding such as UTF-8, and that tokenisation splits text into units such as words.
Why identical text can compare as different
Unicode assigns a code point, a number representing a character in Unicode, to each character. Some visible characters can be represented in more than one valid way.
For example, é can be stored as:
- One code point:
é(the precomposed character) - Two code points:
efollowed by a combining acute accent
A combining character is a code point that modifies the character before it. Both forms normally render as the same user-perceived character. However, Python compares strings by their code points, so the strings are unequal before normalisation.
a = "café" # e with acute as one code point
b = "cafe\u0301" # e followed by a combining acute accent
print(a == b) # False
print(len(a)) # 4
print(len(b)) # 5The strings look the same when displayed, but their internal sequences differ. This difference can cause duplicate database entries, failed searches, or inconsistent filenames.
Canonical equivalence
Unicode calls two representations canonically equivalent when they should be treated as the same text for ordinary comparison. Unicode normalisation converts equivalent representations into a consistent form.
Python provides this operation through unicodedata.normalize:
import unicodedata
a = "café"
b = "cafe\u0301"
b_nfc = unicodedata.normalize("NFC", b)
a_nfd = unicodedata.normalize("NFD", a)
print(b_nfc == a) # True
print(a_nfd == b) # TrueThe first argument selects the normalisation form. The second argument is the string to transform.
NFC: compose where possible
NFC means Normalization Form C. It first decomposes characters into their canonical parts and then recomposes them where a single precomposed character exists.
For example, NFC changes e plus a combining acute accent into é. It is often a good default for stored or displayed text because it keeps common characters compact while preserving their canonical meaning.
import unicodedata
text = "Cafe\u0301"
normalised = unicodedata.normalize("NFC", text)
print(normalised) # Café
print([hex(ord(ch)) for ch in normalised])NFC does not remove accents. The acute accent remains part of the text. It only chooses a consistent representation.
NFD: decompose into base characters and marks
NFD means Normalization Form D. It decomposes characters into their canonical components whenever possible.
For example, é becomes e followed by a combining acute accent. This form is useful when a program needs to inspect or process combining marks separately.
import unicodedata
text = "Café"
decomposed = unicodedata.normalize("NFD", text)
for character in decomposed:
print(character, unicodedata.name(character))The output includes LATIN SMALL LETTER E and COMBINING ACUTE ACCENT. NFD can make a string longer, but it exposes the structure needed for operations such as accent filtering.
| Form | Main result | Typical use |
|---|---|---|
| NFC | Composes canonical equivalents | Storage, display, ordinary comparison |
| NFD | Decomposes canonical equivalents | Inspecting or filtering combining marks |
How the forms relate
A normalisation pipeline can be viewed as a choice between compact canonical characters and explicit components:
NFC and NFD are canonical forms, so neither one is universally more correct. The important rule is consistency. If text is compared, indexed, or used as a key, normalise both values using the same form.
Combining marks and ordering
A character can contain more than one combining mark. Unicode defines a canonical ordering for such marks. Normalisation applies that ordering, so equivalent sequences become consistent even when the marks arrived in different orders.
This matters for multilingual text and user input. A simple character count may still differ from what a user sees. Python's len counts code points, not grapheme clusters. A grapheme cluster is a user-perceived character that may contain several code points.
NFC can reduce some clusters to one code point, but it cannot make every visible character one code point. For example, some emoji sequences and many writing systems require multiple code points even after NFC.
Choosing NFC or NFD
Use NFC when:
- You need stable equality checks or keys.
- You store text for general application use.
- You want common accented characters represented compactly.
- You exchange text with systems that expect a conventional composed form.
Use NFD when:
- You need to examine combining marks.
- You are implementing controlled accent removal.
- A downstream operation specifically expects decomposed text.
Normalisation does not make text language-neutral, transliterate it, or remove punctuation. Those are separate transformations. Also, normalisation should not be confused with encoding. UTF-8 converts code points into bytes; NFC and NFD change the sequence of Unicode code points before or after encoding.
import unicodedata
def canonical_equal(left, right):
return (
unicodedata.normalize("NFC", left)
== unicodedata.normalize("NFC", right)
)
print(canonical_equal("café", "cafe\u0301")) # TrueThis comparison function normalises both inputs to NFC, then compares the results. The same approach can be used before storing text, building search indexes, or checking whether two user-provided values match.
Exercise
Why does "café" == "cafe\u0301" return False in Python, and how can you make the comparison succeed?
Show answer
The strings contain different code-point sequences. Normalise both strings to the same form, such as NFC, with unicodedata.normalize("NFC", value), then compare them.