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.
Code
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 5Inside 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
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Binary Text ConverterConvert UTF-8 text to 8-bit binary groups and decode binary back to text with strict validation.
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`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.