Why len 4 and other weird things you should know about strings in Python
summary
Python reports surprising counts and mismatches because of encoding, normalization and grapheme clusters: len('😶🌫️') == 4 and 'ñ' != 'ñ' happen because code points, variant selectors and zero‑width joiners combine into visible characters. Python uses 1/2/4‑byte internal storage and applies string interning so identity can lie (avoid is, use ==). Use Unicode normalization (NFC/NFD, NFKC/NFKD) and casefold for caseless matches (handles ~297 odd mappings). No built‑in grapheme support — handle visual characters with regex or PyICU.
AI-written summary · about
chapters (15)
- 0:00Unicode strings in Python
- 1:25String encoding basics
- 3:12Unicode and UTF-8
- 5:39Code points and categories
- 6:23Danger of 'is' for strings
- 7:49Caseless comparison pitfalls
- 9:12Case folding for matching
- 10:02Normalization issues
- 12:31Identifier normalization
- 13:16Homoglyphs and confusables
- 14:44Language-specific casing
- 15:05Slicing strings tricky
- 16:51Emojis and grapheme clusters
- 18:39Unicode checklist
- 21:07Q&A
related talks
24mWhy `len('😶🌫️') == 4` and other weird things you should know about strings in Python
33mPython's Transition to Multilingual Programming
35mText Is Not Text: The History of Strings in Python
44mA 🐍's guide to Unicode
51mCharBusters - 10 Unicode Myths
35mPython, Locales and Writing Systems
29mYour Name Is Invalid!
28mMaking Acquaintances with Exotic Encodings
