Intro
If you schedule posts through an API, you will eventually hit a limit counted in graphemes, not characters. Bluesky allows 300 graphemes per post. Mastodon counts 500 characters. X counts 280 (weighted). They are not the same unit, and len() in Python answers a different question.
This is a short technical note about the gap between len() and what the platform actually counts, plus a dependency-free counter we use in production.
The problem with len()
Python's len() returns the number of code points in a string. Most of the time that equals what a reader perceives as characters. Emoji break that assumption.
text = "π¨βπ©βπ§βπ¦"
print(len(text)) # 7
That emoji is a family of four people. A reader sees one glyph. X, Bluesky and most editors count one β Python counts seven, because the glyph is built from four emoji code points joined by three zero-width joiners (U+200D).
The same happens with flags (two regional indicator code points), accented letters written in decomposed form (e + combining acute accent), and emoji with skin tone modifiers.
The consequence is practical: a post that len() says is 295 characters can pass a 300-character check and still be rejected by the API, or the opposite β you trim text you did not need to trim.
Code points, clusters and graphemes
Three different things hide behind "character":
-
Code point β what
len()counts.π¨βπ©βπ§βπ¦is 7. - Grapheme cluster β what a reader perceives as one unit. The same emoji is 1.
- Byte length β what storage and some payload limits count. A Cyrillic letter is 2 bytes in UTF-8.
Social APIs count grapheme clusters, usually following Unicode's extended grapheme cluster rules (UAX #29). The full algorithm is long; most of its practical effect for social posts comes down to four rules:
- Combining marks attach to the previous base character.
- Variation selectors (U+FE0F) do not start a new cluster.
- A zero-width joiner glues the next character to the previous one.
- Two regional indicators form one flag; emoji modifiers (skin tones) attach to the base emoji.
A dependency-free counter
The standard answer is the third-party regex module, which supports \X (grapheme cluster). If you would rather not add a dependency, these four rules are enough for text mixing Latin, Cyrillic and emoji:
import unicodedata
def _skip(ch: str) -> bool:
"""Parts that never start a new grapheme: combining marks,
variation selectors and emoji skin tone modifiers (category Sk)."""
return (unicodedata.combining(ch) > 0
or ch in ("\ufe0f", "\ufe0e")
or 0x1F3FB <= ord(ch) <= 0x1F3FF)
def graphemes(text: str) -> int:
count, i, n = 0, 0, len(text)
while i < n:
ch = text[i]
if ch == "\u200d": # ZWJ glues the next base into the same cluster
i += 2
continue
if 0x1F1E6 <= ord(ch) <= 0x1F1FF: # flags: pair of regional indicators
count += 1
i += 1
if i < n and 0x1F1E6 <= ord(text[i]) <= 0x1F1FF:
i += 1
continue
if _skip(ch):
i += 1
continue
count += 1
i += 1
while i < n and _skip(text[i]):
i += 1
return count
Results
Measured with Python 3.14 on the strings below:
| String | len() |
graphemes() |
|---|---|---|
ΠΡΠΈΠ²Π΅Ρ, ΠΌΠΈΡ! |
12 | 12 |
πΊπ¦ (flag) |
2 | 1 |
π¨βπ©βπ§βπ¦ (family, ZWJ) |
7 | 1 |
e + combining accent |
2 | 1 |
ππ½ (skin tone) |
2 | 1 |
β€οΈ (heart + VS16) |
2 | 1 |
A mixed example: Ready for Black Friday π¨βπ©βπ§βπ¦πΊπ¦ππ½ is 34 code points but 26 graphemes β an 8-character difference that a naive length check would charge you for.
Limitations
Be honest about the edges:
- This is not a full UAX #29 implementation. Indic and Arabic clusters that depend on viramas and other invisible joiners are counted character by character.
- Emoji tag sequences (subdivision flags) and family emoji with more than one modifier can still be miscounted.
- URL shorteners and previews are not handled β most platforms count a link as a fixed length, not as its literal string.
If exactness matters (for example, when you bill by characters), use regex.findall(r"\X", text) or a dedicated grapheme library and treat this snippet as a fast pre-check.
Conclusion
len() answers "how many code points", not "how many characters a human sees". For any publishing pipeline that touches emoji, flags or decomposed accents, count graphemes before you count yourself safe. Four rules and twenty lines of standard library code cover the common cases; a proper library covers the rest.
This article was prepared with AI assistance. The code and the measurements above were run on Python 3.14 and are reproducible.
Top comments (0)