One facepalm emoji has length 5 in Python, 7 in JavaScript and 17 bytes on disk. Here is why, plus the Turkish I bug.
Quick question. How long is the text "🤦🏽♂️"? You see one facepalm. JavaScript reports 7, Python reports 5, and the file on disk takes 17 bytes.
Nobody is wrong. Each one counts a different thing, and that is what makes text secretly hard. Let's look at the layers one at a time.
A computer can't store a letter. It stores numbers, and we agree on a table that lists which number means which letter. In the standard table, 65 means "A" and 66 means "B".
Unicode is the giant shared table. Each entry gets a number called a code point, written like U+0041 for "A". It covers scripts from all over the world, plus emoji.
The number Unicode gives to one entry in its table. "A" is U+0041. The Unicode range tops out at U+10FFFF.
So a code point is a number in a table. But that's not what lands in your file.
A code point is an idea. A byte is what actually sits in your file (one byte holds a number from 0 to 255). So we need a rule for turning code points into bytes. That rule is an encoding, and the popular one is UTF-8.
How does UTF-8 do it?
UTF-8 uses 1 to 4 bytes per code point. Small numbers get fewer bytes, big numbers get more. The rules are in RFC 3629.
| Code point range | Bytes |
|---|---|
| U+0000 to U+007F | 1 |
| U+0080 to U+07FF | 2 |
| U+0800 to U+FFFF | 3 |
| U+10000 to U+10FFFF | 4 |
The first range is plain old ASCII, stored as one byte exactly like before. So English text costs 1 byte per letter, and old files stay valid. That is a big reason UTF-8 won.
Now we have enough to crack the facepalm.
The facepalm is built from several code points glued together. Here is the list, step by step:
That's 5 code points and 17 bytes. JavaScript counts in UTF-16 units, where the first two code points take 2 units each, so you get 7. Python counts code points, so 5.
Click Next to see the same facepalm counted by each layer. The number changes at each stop.
You see one facepalm emoji
So "length" has no single answer until you say what you're counting. And it gets worse with plain letters.
Even "é" can be one code point or two: an "e" plus a combining accent mark. Same look, different length. Comparing them with == can report "not equal" for text that looks identical.
Turkish has two letters I: one with a dot and one without. The pairs are I with ı (dotless), and İ (dotted capital) with i. English only knows I with i.
So lowercasing "I" depends on the language. In English it gives "i". In Turkish it gives "ı". Unicode even keeps special rules for this in its SpecialCasing file.
The classic bug: code lowercases a keyword like "FILE" to compare it, runs on a machine set to Turkish, and the match fails. The cause is a small difference you cannot see.
The reverse can surprise you too. In Python, 'İ'.lower() isn't one letter. It gives i plus a combining dot, so two code points.
I used to think a string was a list of letters. It's really a stack of layers, and each layer has its own count.
.lower() and hope.