Built-in Functions and Types

len()

Count items in a sized object. For text, the count is Unicode code points, not encoded bytes or visual graphemes. Dictionaries count keys, and nested collections count only their outer items. An iterator need not have a length. Custom types implement __len__(); unsupported objects raise TypeError. On CPython a length exceeding sys.maxsize raises OverflowError.

Formal Syntax & Structure

python syntaxpython
len(obj, /)

Parameters & Attributes

NameTypeDescription
objsized objectObject supplying a length; positional only.

Return Value

int

Nonnegative item count, not a recursive count.

Working Example

Example Snippetpython
print(len(["pen", "book"]))print(len("é"))print(len({"a": 1, "b": 2}))
Python Reference