len Counts Code Points And encode Counts Bytes

len on str counts code points while len of the UTF-8 encoding counts bytes, so the same text reports different sizes at the language and transport layers.

PythonForm Validation

Code

pythonsnippet
11 lines
emoji = "🙂"
print("chars:", len(emoji))
print("utf-8 bytes:", len(emoji.encode("utf-8")))

text = "naïve café"
print("chars:", len(text))
print("utf-8 bytes:", len(text.encode("utf-8")))

# in UTF-8 an ASCII character takes 1 byte, accented letters 2, emoji 4
print("ascii:", len("cafe"), len("cafe".encode("utf-8")))
print("accents:", len("café"), len("café".encode("utf-8")))

Output

Captured from a sandboxed run of this snippet in the build pipeline.

chars: 1
utf-8 bytes: 4
chars: 10
utf-8 bytes: 12
ascii: 4 4
accents: 4 5

Inside the snippet

Every operation runs against top-level bindings: emoji, text.

What to watch for

  • The dependency list is empty — the language runtime already provides everything.

Using it

Each step the file takes is announced in the output: chars:, utf-8 bytes:, ascii:.

Interactive Tools for This Topic

FAQ

Does this work in a bare project with no dependencies file?
Yes — a bare project runs it as-is, since nothing needs declaring. Its state lives in module-level bindings: `emoji`, `text`.