What built-in function returns the length of an object in Python?

The story behind the answer

The built-in Python function that returns an object's length is len.

Called as len(object), it reports the number of items in a sequence or collection. It works with familiar objects such as strings, lists, tuples, dictionaries, sets, and ranges: len("Python") returns 6, while len({"a": 1, "b": 2}) returns 2. For a dictionary, the count is of keys, not individual values.

Python makes len broadly useful through the __len__ special method. Custom classes can define __len__ so their instances respond to len(). The function asks the object for that size rather than requiring a separate length routine for every data type.

A common mix-up is assuming len() measures memory usage, character display width, or the number of bytes. It does none of those. For text, it counts Python string elements, while visual characters such as emoji sequences can involve multiple code points. len() also differs from count(), which searches for occurrences of a particular value.

Source: Wikipedia · fact-checked Aug. 2026

Add question to a list

Choose a list to keep this question in: