Post

Python len() Method

In this tutorial we will learn about the python len() method and its uses with examples.

Python len() Method

What is the python len() Method?

The Python len() method is a built-in Python method that returns the object’s length. It’s also known as the python length method.

The syntax of python len() is

1
2
len(object)

Python len() Parameters Method

The len() method takes only one parameter as an argument.

  • object - the name of the object that can be a sequence or a collection.

len() method can be used with almost all the objects like string, integer, tuple, list, range, dictionary, set, list, etc.

Example 1: How to use the len() method in python?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# string

b = "We love Python"
print("The length of b is:", len(b))

# list

my_list = [1,2,3,4,5]
print("The length of my_list is:", len(my_list))

# tuple

my_tuple = ("Apple","Banana","Orange")
print("The length of my_tuple is:",len(my_tuple))

Output:

1
2
3
4
The length of b is: 14
The length of my_list is: 5
The length of my_tuple is: 3

We can also use the len() method and the range() method to check the length of the range.

Example 2: How to use len() with range() method?

1
2
3
4
# range

print("The length of range method is",len(range(1,100)))

Output:

1
2
The length of range method is 99

Example 3: How to use the len() method with dictionaries and sets?

1
2
3
4
5
6
7
8
9
10
11
# set

my_set = {1,2,3,4,5}
print("The length of the my_set is",len(my_set))


# dictionary

my_dict = {1:'Apple',2:'Banana',3:'Orange'}
print("The length of the my_dict is",len(my_dict))

Output:

1
2
3
The length of the my_set is 5
The length of the my_dict is 3

Rules of len() method

  • If a non-sequence object is passed, it will raise a TypeError exception because the len() method cannot deal with non-sequence objects.
This post is licensed under CC BY 4.0 by the author.