Reference · General
len
signature
len(str | list | map | html | null) -> intWhat it does
The size of a value: a string's characters, a list's items, a map's keys, and an
html value's bytes. null has length 0, so a missing list or text counts as empty.
x.len is the same as len(x).
Examples
print(len("hello"), len([1, 2, 3]), len({ a: 1, b: 2 }))print(len(""), len([]), len({}))output
5 3 20 0 0Edge cases
- Characters, not bytes:
len("é")andlen("😀")are 1. nullis 0, not an error.- A list counts its items only, not what is inside them:
[[1, 2], [3]]has length 2. - An
htmlvalue's length is its bytes, as sent to the browser. - Splitting the empty string gives one empty piece, so its length is 1, not 0.
print(len("é"), len("😀"), len("héllo"), len("a\nb"))print(len(null), len([[1, 2], [3]]))print(len(<b>hi</b>))print(len(split("", ",")))output
1 1 5 30 291Errors
A number, a boolean or a function has no length: that is a runtime error, not an error
value. Convert a number with str first.
this one fails
print(len(42))output
line 1: `len` expects a str, list or map, got intChaining
print(123 | str | len)print("a,b,c" | split(",") | len)print(["ab", "", "c"] | filter((s) => len(s) > 0) | len)output
332See also
slice, split, count, str, contains
Thread
Thread 0 posts
On len(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo