prize18-4 Exercise 2: Counting UTF-16 Units ============================================= Idea: a code point at or below U+FFFF is one 16-bit unit; anything above needs a surrogate pair, two units. len() counts code points, so the two numbers differ exactly when the string contains characters above U+FFFF. The family emoji is five code points: three people (each above U+FFFF, so 2 units) plus two joiners (U+200D, below U+FFFF, so 1 unit each): 3 x 2 + 2 = 8. ex2.py: def utf16_units(s): return sum(2 if ord(c) > 0xFFFF else 1 for c in s) samples = ["hello", "caf\u00e9", "\u20ac5", "\U0001F600", "a\U00010437b", "\U0001F468\u200D\U0001F469\u200D\U0001F467"] for s in samples: check = len(s.encode("utf-16-le")) // 2 print(f"{s!r:12} code points={len(s)} utf-16 units={utf16_units(s)} agrees={utf16_units(s) == check}") Real output (Python 3.14): 'hello' code points=5 utf-16 units=5 agrees=True 'café' code points=4 utf-16 units=4 agrees=True '€5' code points=2 utf-16 units=2 agrees=True '😀' code points=1 utf-16 units=2 agrees=True 'a𐐷b' code points=3 utf-16 units=4 agrees=True '👨\u200d👩\u200d👧' code points=5 utf-16 units=8 agrees=True Why it matters: in a language that reports UTF-16 units as the string length, a single emoji has length 2 and this family has length 8, while a person would say the family is one character. (Which languages do this is from general knowledge: check.)