Python len() function
Return the number of items in a string, list, tuple, dictionary, set or range.
Syntax
len(obj)
For a string it counts characters (including spaces), for a dictionary the number of keys. It works on anything that has a size. len is instant even for huge lists.
Parameters
| obj | a sized collection or string |
Return value
int
Example
Code
print(len("Brisbane")) print(len([3, 1, 4, 1, 5])) print(len({"a": 1, "b": 2})) print(len(""), len([])) words = "the quick brown fox".split() print(len(words), "words")
Output
8 5 2 0 0 4 words
Good to know. The last index of a list is
len(xs) - 1, not len(xs).Related
Learn it step by step in the free lesson Lists & Tuples, with exercises that check your answers.