Python len() Function

Last Updated : 1 Jul, 2026

len() function returns the number of items present in an object. It can be used with strings, lists, tuples, dictionaries, sets and other container types to determine their size.

Python
text = "Programming"
print(len(text))

Output
11

Explanation: len(text) calculates the number of characters in the string, "Programming" contains 11 characters and function returns 11, which is then displayed using print().

Syntax

len(object)

  • Parameter: object is a sequence (such as a string, list, tuple) or collection (such as a dictionary, set) whose length is to be calculated.
  • Returns: An integer value indicating the number of items in the object.

Examples

Example 1: In this example, we use the len() function to find the number of elements in a list, tuple and dictionary.

Python
fruits = ["Apple", "Mango", "Orange", "Grapes"]
print(len(fruits))

numbers = (10, 20, 30, 40)
print(len(numbers))

student = {"name": "Emma", "age": 21, "city": "London"}
print(len(student))

Output
4
4
3

Explanation:

  • len(fruits) returns 4 because the list contains four elements.
  • len(numbers) returns 4 because the tuple contains four values.
  • len(student) returns 3 because the dictionary contains three key-value pairs.

Example 2: In this example, we use the len() function to find the length of an empty list.

Python
a = []
print(len(a)) 

Output
0

Explanation: Since the list is empty, len() returns 0, indicating there are no elements inside it.

Example 3: In this example, we use len() with range() to access and display each element of a list using its index.

Python
a = [10, 20, 30, 40, 50]
for i in range(len(a)):
    print("Index:", i, "Value:", a[i])

Output
Index: 0 Value: 10
Index: 1 Value: 20
Index: 2 Value: 30
Index: 3 Value: 40
Index: 4 Value: 50

Explanation: range() and len() are used to iterate over the list a by index. len(a) gives the total number of elements and range(len(a)) provides the index sequence. In each iteration, a[i] accesses the value at index i.

Comment