December 5, 2026

Unicode Normalization Explained: NFC vs NFD vs NFKC, and Why “café” Doesn’t Equal “café”

Two identical blank shop signs side by side on a brick wall

Two strings print as café. Python says they are not equal. One has a length of 4, the other a length of 5, and a Linux directory will happily hold two files with those two names side by side. Nothing is corrupted. Unicode allows the same visible letter to be stored more than one way, and normalization is the step that picks one. Tests in Python 3.13 (Unicode database 15.1), Chrome’s JavaScript engine and git 2.43 show where the duplicates come from, which normalization form fixes which problem, and where the most aggressive form quietly changes text.

Video: Dylan Beattie on YouTube

Two ways to write é

Precomposed and combining

The letter é can be stored as a single code point, U+00E9, LATIN SMALL LETTER E WITH ACUTE. It can also be stored as two: a plain e (U+0065) followed by U+0301, COMBINING ACUTE ACCENT, which attaches itself to the letter before it. Both render identically.

Python’s unicodedata documentation gives the same kind of example with a capital C: U+00C7, C with cedilla, “can also be expressed as the sequence U+0043 (LATIN CAPITAL LETTER C) U+0327 (COMBINING CEDILLA).” Python unicodedata documentation

What the computer sees

In the test, "café" and "café" compared as not equal. Their lengths were 4 and 5. Encoded as UTF-8 they were different bytes too, caf\xc3\xa9 against cafe\xcc\x81. Chrome agreed: === returned false, the lengths were 4 and 5, and encodeURIComponent produced two different URL encodings, caf%C3%A9 and cafe%CC%81.

MDN’s reference for JavaScript’s normalize() describes such pairs as canonically equivalent, meaning they “represent the same abstract characters, and should always have the same visual appearance and behavior,” but notes that because the code points differ, “string comparison will not treat them as equal.” MDN, String.prototype.normalize()

Black usb cable on white table

The four normalization forms

Canonical: NFC and NFD

Python’s documentation describes the two canonical forms. Normal form D, NFD, “translates each character into its decomposed form.” Normal form C, NFC, “first applies a canonical decomposition, then composes pre-combined characters again.”

Applied to both spellings of café, NFC turned each into the 4-character version ending in U+00E9, and NFD turned each into the 5-character version ending in e plus U+0301. After normalizing to the same form, the two strings compared equal in both Python and JavaScript. MDN notes that NFC is the default form for normalize() when none is given.

Compatibility: NFKC and NFKD

The other two forms go further. The Python documentation explains that Unicode includes certain characters “which normally would be unified with other characters,” using U+2160, ROMAN NUMERAL ONE, as an example that “is really the same thing as” U+0049, the ordinary Latin capital. NFKD replaces such compatibility characters with their plain equivalents, and NFKC does the same followed by canonical composition.

Beyond accented Latin letters

The difference in size can be much larger than one code point. The Korean word 한국어 is three characters in NFC. Under NFD each syllable split into its component jamo, and the word became 8 code points and 24 bytes of UTF-8, against 9 bytes for the NFC form. Text that passes through a system storing NFD can nearly triple in size without any visible change.

Vietnamese shows a second wrinkle. The letter ệ carries two marks, a dot below and a circumflex. Written as e followed by the dot (U+0323) and then the circumflex (U+0302), it is three code points. Written with the two marks in the opposite order, it is a different string, and a plain comparison returned False. NFC turned both orders into the same single code point, U+1EC7, and they compared equal. Normalization does not only compose characters; it also puts combining marks into a defined order, so text built by different keyboards or libraries ends up identical.

What compatibility normalization changes

Tested characters

The test ran several characters through NFC and NFKC:

Input NFC NFKC
file (fi ligature) file file
① (circled one) ① 1
x² (superscript two) x² x2
AB (fullwidth) AB AB
Ⅻ (Roman numeral twelve) Ⅻ XII

NFC left every one alone. NFKC flattened them all into ordinary ASCII.

Why that is risky

That flattening is useful for search, where a user typing file should find a document containing the ligature. It is destructive for storage. x² and x2 mean different things, and NFKC erases the difference permanently. A database that applies NFKC to everything on the way in has quietly rewritten its users’ text.

The safe general rule is to store text in NFC, which never changes how text looks, and reserve NFKC for building search keys, usernames or other identifiers where look-alike characters should collapse together.

One canonical surprise

Canonical normalization is not entirely invisible either. U+212B, ANGSTROM SIGN, normalized under NFC to U+00C5, LATIN CAPITAL LETTER A WITH RING ABOVE. They look the same, and Unicode treats them as canonically equivalent, so even NFC replaces the first with the second.

Close-up of a glowing electronic circuit board with lights

Where unnormalized text causes bugs

Duplicate file names

Linux file systems store names as bytes and do not normalize them. The test created one file named with the precomposed é and another named with the combining sequence. os.listdir() returned both, displayed as café and café, with name lengths of 4 and 5. Running git status in that directory listed two untracked files, shown with their escaped bytes as "caf\303\251" and "cafe\314\201".

Files copied between systems that store names differently, or names typed on different keyboards and input methods, can end up as this kind of invisible pair. A script that looks a file up by name will find one and miss the other.

Lookups and sorting

A membership test, "café" in ["café"], returned False. Dictionary keys, set membership, database unique constraints and cache keys all behave the same way: unnormalized input creates duplicates or misses.

Plain sorting is affected too. Python’s default sort placed cafes ahead of both versions of café, because sorting compares code points one at a time. Against the precomposed spelling, the fourth character decides it: e (U+0065) in cafes sorts before é (U+00E9). Against the decomposed spelling, the first four characters match and the fifth decides it: s (U+0073) sorts before the combining accent (U+0301). Either way, an accented word lands after a word that a human would file beside it. Locale-aware comparison behaves differently: in Chrome, localeCompare and Intl.Collator both returned 0 for the two spellings, treating them as equal without explicit normalization.

Counting characters

Length depends on representation, and on what is being counted. JavaScript’s [...str] split the decomposed café into 5 code points. Intl.Segmenter, which splits text into user-perceived characters, counted 4. A form field that limits input to a number of characters can reject or accept the same visible word depending on how it was typed and how the limit is measured.

Python normalizes identifiers, not strings

Source code is NFKC

Python’s language reference states that “All names are converted into the normalization form NFKC while parsing.” Python language reference, Lexical analysis The documentation’s own example shows a name written in decorative characters being treated as finalization.

The test confirmed it with a smaller example. A file assigned fix = 5, with the fi ligature, and then printed fix, spelled normally. It printed 5. Checking the module’s globals showed the stored name was the plain fix: "fix" in globals() returned False and "fix" in globals() returned True.

Only at parse time

The same reference adds that “Normalization is done at the lexical level only. Run-time functions that take names as strings generally do not normalize their arguments.” That is exactly what the globals check showed: the dictionary lookup with the ligature string failed because nothing normalized it.

NFKC also does not make every character legal. A test file containing x² = 3 failed with SyntaxError: invalid character '²' (U+00B2), because the superscript is not allowed in an identifier in the first place.

A dramatic lightning strike illuminates stormy clouds

Normalizing in practice

Python

unicodedata.normalize('NFC', s) returns the normalized string, and unicodedata.is_normalized('NFC', s), added in Python 3.8, checks without building a new string. In the test, the precomposed café returned True for NFC and the decomposed one returned False.

JavaScript

str.normalize() with no argument applies NFC. str.normalize('NFKC') applies the compatibility form. In Chrome, normalizing both spellings of café with the default form made them strictly equal, and the decomposed one’s length dropped from 5 to 4, matching the Python results exactly.

Where to apply it

  • Normalize to NFC at input boundaries: form submissions, file and database imports, API requests.
  • Compare and index on a normalized form, and use NFKC plus case folding for search keys and usernames.
  • Leave user content in NFC for storage and display.
  • Normalize file names before creating files when names come from user input.

Case is a separate step. "Straße".casefold() and "STRASSE".casefold() compared equal in the test, while "fi".upper() returned FI, two characters from one. Neither of those is normalization, but both change lengths and equality in the same kinds of places.

Sources

  • Python documentation, unicodedata: Unicode Database: https://docs.python.org/3/library/unicodedata.html
  • Python Language Reference, Lexical analysis: https://docs.python.org/3/reference/lexical_analysis.html
  • MDN Web Docs, String.prototype.normalize(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize

920 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *