What is the name of Python's built-in function to get the length of a sequence?

The story behind the answer

Python’s built-in function to get the length of a sequence is len. Calling len(sequence) returns the number of items, while len(string) returns the number of characters in that string.

The function works with built-in sequences such as lists, tuples, strings, ranges, and bytes. For example, len([10, 20, 30]) returns 3, and len("Python") returns 6. It also works with many collections that define a size, including dictionaries and sets, although those are not sequences in the strict technical sense.

Under the hood, len usually uses an object’s __len__() special method. Custom classes can support len by defining that method and returning a non-negative integer. This is why len is more general than a method belonging only to lists or strings.

A frequent mistake is choosing count, size, or length as though they were universal Python built-ins. count is commonly a method, such as list.count(value), that counts occurrences of one value. size and length are not the standard general-purpose function names. For iterators without a known stored size, programmers generally must consume or otherwise process the items to count them.

Source: Wikipedia · fact-checked Aug. 2026

Add question to a list

Choose a list to keep this question in: