Skip to the content.
Mastering Unicode Normalization for Robust Text Processing | AI Systems Design From Scratch

Connect with Amin Boulouma Official

🏠 Documentation Hub 📝 Engineering Blog 💻 GitHub Repository

Mastering Unicode Normalization

Amin Boulouma, Software Engineer

In global applications, text input is rarely uniform. Users might submit identical-looking characters that are encoded differently in Unicode, or you might need to strip special characters to normalize inputs for database keys or URL slugs. Python’s unicodedata module provides the essential tools to handle these variations reliably.

The Problem: The “Visual vs. Logical” Mismatch

In Unicode, “é” can be represented as a single code point (\u00e9) or as a base “e” plus a combining acute accent (e + \u0301). To a computer, these are different strings, breaking search functions, equality checks, and database lookups.

1. Stripping to ASCII: The NFKD Strategy

When generating slugs or sanitizing filenames, you often need to convert “foreign” characters into their closest ASCII equivalents.

import unicodedata

def slugify(text: str) -> str:
    # NFKD decomposes characters into base chars and combining accents
    # Then we encode as ASCII and ignore non-encodable characters
    text = unicodedata.normalize('NFKD', text)
    return text.encode('ascii', 'ignore').decode('ascii')

# Example: "café" -> "cafe"

2. Standardizing Representation: The NFKC Strategy

When you want to maintain the character’s integrity but ensure that all inputs follow a standardized format (e.g., converting “full-width” numbers/letters into standard counterparts), use NFKC.

# NFKC (Normalization Form Compatibility Composition)
# This standardizes visually compatible characters
text = unicodedata.normalize('NFKC', text)

Comparison of Normalization Forms

Form Full Name Use Case
NFC Normalization Form C Default for most databases/APIs (Standardized)
NFD Normalization Form D Good for stripping accents via NFKD
NFKC Compatibility Composition Standardizing input for search/matching
NFKD Compatibility Decomposition Creating clean ASCII slugs

Best Practices

By applying the correct normalization form, you ensure your text processing is resilient, platform-independent, and ready for global user inputs.

Connect with Amin Boulouma Official