Returns the number of items of an object
Usage
The len() function returns the number of items of an object.
The object may be a sequence (such as a string, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).
Syntax
len(object)
| Parameter | Condition | Description |
| object | Required | A sequence or a collection. |
len() on Sequences
# number of characters in a string
S = 'Python'
x = len(S)
print(x)
# Prints 6# number of items in a list
L = ['red', 'green', 'blue']
x = len(L)
print(x)
# Prints 3# number of items in a tuple
T = ('red', 'green', 'blue')
x = len(T)
print(x)
# Prints 3len() on Collections
# number of key:value pairs in a dictionary
D = {'name': 'Bob', 'age': 25}
x = len(D)
print(x)
# Prints 2# number of items in a set
S = {'red', 'green', 'blue'}
x = len(S)
print(x)
# Prints 3