Built-in Functions
len()
Count the items in a collection or the characters in a string.
len(object)
len() returns the number of items in an object (such as a string, list, tuple, dictionary, or set).
Parameters
| Name | Description |
|---|---|
| object | A sized value such as a string, list, tuple, set, or dictionary. |
Returns
An integer count.
Try it
Run it and change it
words = ["red", "blue", "green"] print(len(words))
Worth knowing
- Constant time O(1): For Python's built-in collections,
len()reads an internal size counter inO(1)time without counting elements.