Normalize human names with respect for Unicode and cultural variation.
How It Works
Names contain accents, combining marks, apostrophes and writing systems other than ASCII. Unicode normalization can make equivalent representations consistent without discarding characters. Do not assume title() or splitting on a space can produce culturally correct names.
For real identities, retain the user-provided form for display and avoid forcing a universal name format.
Step-by-Step Example
import unicodedata
name_a = "Jos\u00e9"
name_b = "Jose\u0301"
print(name_a == name_b)
normalized_a = unicodedata.normalize("NFC", name_a)
normalized_b = unicodedata.normalize("NFC", name_b)
print(normalized_a == normalized_b)
Understanding the Example
- The expression '"Jos\\u00e9"' supplies the value stored as 'name_a'. This value is then used or checked by the later statements.
- The expression '"Jose\\u0301"' supplies the value stored as 'name_b'. This value is then used or checked by the later statements.
A successful run of this example prints the following output:
False
True
Where This Fits in a Real Program
Start by writing down examples that should match and examples that must fail. Check whether you need to locate a substring, match a prefix or validate an entire field; these are different operations. Unicode and escape sequences can also change which characters are matched, so test accented text and empty input.
Practice: Try It Yourself
Normalize two equivalent accented spellings and compare their code-point lengths.
For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.
Further Reading
Read the official Python documentation for complete parameters, return types and exceptions.
Common Beginner Mistakes
- Removing all non-ASCII characters as a supposed sanitization step for personal names.
- Assuming that case changes and whitespace rules are identical across all languages.
Key Takeaways
- Normalize human names with respect for Unicode and cultural variation.
- Names contain accents, combining marks, apostrophes and writing systems other than ASCII.
- For real identities, retain the user-provided form for display and avoid forcing a universal name format.