len()
is a function that calculates the length of the object within the parenthesis. For a string, it will count the number of characters: len("hello")
will return 5
, and len("hi there")
will return 8
, because it counts blank spaces. For a list, len()
will count the number of list items:
>>> groceries = ["berries", "bananas", "oat milk", "eggs", "spinach", "bell peppers", "ice cream", "frozen pizza", "floss", "hand soap"]
>>> len(groceries)
10
- Read more about
len()
and more things it can calculate.
- Check out
len()
on w3schools.