This guide explains String Length vs Visible Character Count through a focused example and practical reasoning. No prior knowledge of this specific operation is required.
Understanding the Concept
len(text) counts Unicode code points, not necessarily glyphs that a person sees as single characters. Combining accents and some emoji sequences can contain multiple code points; UTF-8 bytes have another length.
When You Would Use This
Compare a visibly similar accented word written in two Unicode forms.
Step-by-Step Example
first_word = "é"
second_word = "é"
print(len(first_word), len(second_word))
print(first_word == second_word)
How the Example Works
The first form has one code point and the second has two; they compare unequal before Unicode normalization even though they may look the same on screen.
Example Output
1 2
False
Try It Yourself
Use unicodedata.normalize("NFC", ...) to compare both examples after normalization.
Common Beginner Mistakes
- Treating
len()as the visible-character count for all languages and emoji. - Comparing differently normalized text without normalization when appropriate.
- Confusing Unicode code points with UTF-8 bytes.
Key Takeaways
len(text)counts Unicode code points, not necessarily glyphs that a person sees as single characters.- The first form has one code point and the second has two; they compare unequal before Unicode normalization even though they may look the same on screen.
- Avoid this mistake: Treating
len()as the visible-character count for all languages and emoji.