len
Count the items of a collection or the characters of a string.
Python 3.13
len(s)- s
- The object to measure: a string, list, tuple, dictionary, set, range and so on.Sized
Return value
A non-negative integer.
Examples
String length
Python 3.13
print(len("hello"))5
List length
Python 3.13
print(len([1, 2, 3]))3
Number of dictionary keys
Python 3.13
print(len({'a': 1, 'b': 2}))2
Details
Raises TypeError for an object without a length, such as a number or an iterator.
