On this page
unicodedata
Prev: typing | Up: stdlib_module | Next: unittest
Rel: string, re, difflib, textwrap, stringprep
Ref: docs.python.org — unicodedata
Tags: #public #python3 #stdlib
Import
import unicodedata
Overview
Full Unicode Character Database access (normalize, category, name, lookup, east_asian_width, combining). Essential for normalizing text before comparison or indexing (NFC/NFKC), stripping diacritics cleanly, classifying punctuation/control characters, and measuring East Asian wide characters in terminal UIs.
Quick Start
import unicodedata
# Composed 'é' vs decomposed 'e' + combining acute accent:
composed = "\u00e9"
decomposed = "e\u0301"
assert composed != decomposed
assert unicodedata.normalize("NFC", decomposed) == composed
assert unicodedata.name("λ") == "GREEK SMALL LETTER LAMDA"
Implementation Walkthrough
1. Stripping Diacritics / Accents for Search & Slugs
Decompose with NFD and drop combining marks (category == "Mn"):
import unicodedata
def strip_accents(text: str) -> str:
decomposed = unicodedata.normalize("NFD", text)
return "".join(ch for ch in decomposed if unicodedata.category(ch) != "Mn")
assert strip_accents("café naïve résumé") == "cafe naive resume"
2. Detecting Wide Characters for Terminal Alignment
import unicodedata
def is_wide(ch: str) -> bool:
return unicodedata.east_asian_width(ch) in {"W", "F"}
assert is_wide("漢") and not is_wide("A")
Gotchas & Boundaries
- Always normalize Unicode before comparing filenames or user identifiers: macOS HFS+/APFS often stores filenames in NFD while Linux/Windows use NFC—normalize to
NFCbefore comparing. - Use
NFCfor canonical storage,NFKCfor fuzzy search/compatibility:NFKCfolds superscripts (²→2) and ligatures (fi→fi), which changes exact display text. unicodedata.name(ch)raisesValueErrorfor control characters or unassigned code points: Pass a default (unicodedata.name(ch, "")).
Common Functions
- unicodedata.bidirectional() —
bidirectional(chr, /)— Returns the bidirectional class assigned to the character chr as string. - unicodedata.category() —
category(chr, /)— Returns the general category assigned to the character chr as string. - unicodedata.combining() —
combining(chr, /)— Returns the canonical combining class assigned to the character chr as integer. - unicodedata.decimal() —
decimal(...)— Converts a Unicode character into its equivalent decimal value. - unicodedata.decomposition() —
decomposition(chr, /)— Returns the character decomposition mapping assigned to the character chr as string. - unicodedata.digit() —
digit(...)— Converts a Unicode character into its equivalent digit value. - unicodedata.east_asian_width() —
east_asian_width(chr, /)— Returns the east asian width assigned to the character chr as string. - unicodedata.is_normalized() —
is_normalized(form, unistr, /)— Return whether the Unicode string unistr is in the normal form 'form'. - unicodedata.lookup() —
lookup(name, /)— Look up character by name. - unicodedata.mirrored() —
mirrored(chr, /)— Returns the mirrored property assigned to the character chr as integer. - unicodedata.name() —
name(...)— Returns the name assigned to the character chr as a string. - unicodedata.normalize() —
normalize(form, unistr, /)— Return the normal form 'form' for the Unicode string unistr. - unicodedata.numeric() —
numeric(...)— Converts a Unicode character into its equivalent numeric value.
Classes & Objects
- unicodedata.UCD —
unicodedata.UCDclass. (Methods:.bidirectional(),.category(),.combining(),.decimal(),.decomposition(),.digit())
Constants & Attributes
unicodedata.ucd_3_2_0(UCD) =<unicodedata.UCD object at 0x109dd0ac0>unicodedata.unidata_version(str) ='16.0.0'
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/difflib
- tuts/python3/stdlib_module/re
- tuts/python3/stdlib_module/readline
- tuts/python3/stdlib_module/rlcompleter
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/string
- tuts/python3/stdlib_module/stringprep
- tuts/python3/stdlib_module/textwrap
- tuts/python3/stdlib_module/typing
- tuts/python3/stdlib_module/unicodedata/bidirectional
- tuts/python3/stdlib_module/unicodedata/category
- tuts/python3/stdlib_module/unicodedata/combining
- tuts/python3/stdlib_module/unicodedata/decimal
- tuts/python3/stdlib_module/unicodedata/decomposition
- tuts/python3/stdlib_module/unicodedata/digit
- tuts/python3/stdlib_module/unicodedata/east_asian_width
- tuts/python3/stdlib_module/unicodedata/is_normalized
- tuts/python3/stdlib_module/unicodedata/lookup
- tuts/python3/stdlib_module/unicodedata/mirrored
- tuts/python3/stdlib_module/unicodedata/name
- tuts/python3/stdlib_module/unicodedata/normalize
- tuts/python3/stdlib_module/unicodedata/numeric
- tuts/python3/stdlib_module/unicodedata/UCD
- tuts/python3/stdlib_module/unittest
Related notes
- tuts/python3/stdlib_module/difflib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/re Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/string Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stringprep Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/textwrap Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/typing Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/unicodedata/bidirectional Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/unicodedata/category Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/unicodedata/combining Linked here; links to this page; shared tags: #stdlib