len()

Count the characters in a string or the items in a list, dictionary or other collection.

Syntax

len(collection)
  • The last position of a list is len(list) - 1.

Example

print(len("robot"))
print(len([1, 2, 3]))

Output:

5
3

Common mistakes

  • len() of a number is a TypeError: convert it to a string first.

Practise it