Javatpoint Logo
Javatpoint Logo

Python len() Function

Python is a high-level, general-purpose programming language that is widely used in different fields like data science, web development, and automation. Python gives many built-in functions that make programming more straightforward and quicker. One such helpful function is len(), which is used to get the length of an object.

The python len() function is used to return the length (the number of items) of an object. The len() function takes an argument, which can be any object that can be estimated, and returns the number of elements in that object. The argument can be a string, list, tuple, set, word reference, or some other iterable object.

Syntax:

Parameters

object: An object that must be a sequence(string, tuple, list etc.) or a collection(dictionary, set etc.)

Return

It returns the length (the number of items) of an object.

Python len() Function Example 1

The below example shows the length of string. In this example, we'll utilize the len() function to get the length of a string.

Code

Output:

10

Explanation:

In the above code, we've defined a string variable named string that contains the text "Hi, World!". We've then, at that point, passed this string variable as an argument to the len() function, which returns the number of characters in the string. The result is 10, which is the length of the string.

Python len() Function Example 2

The below example shows the length of list. In this example, we'll utilize the len() function to get the length of a list.

Code

Output:

5

Explanation:

In the above code, we've defined a list variable named my_list that contains the numbers 1 through 5. We've then, at that point, passed this list variable as an argument to the len() function, which returns the number of elements in the list. The result is 5, which is the length of the list.

Python len() Function Example 3

The below example shows the length of dictionary. In this example, we'll utilize the len() function to get the number of key-esteem matches in a dictionary.

Code

Output:

3

Explanation:

In the above code, we've defined a dictionary variable named my_dict that contains some key-esteem matches. We've then passed this dictionary variable as an argument to the len() function, which returns the number of key-esteem matches in the dictionary. The result is 3, which is the length of the dictionary.

Python len() Function Example 4

The below example shows the length of set. In this example, we'll utilize the len() function to get the number of elements in a set.

Code

Output:

5

Explanation:

In the above code, we've defined a set variable named my_set that contains the numbers 1 through 5. We've then passed this set variable as an argument to the len() function, which returns the number of elements in the set. The result is 5, which is the length of the set.

Python len() Function Example 5

We can utilize the len() function with our custom objects. To do this, we want to characterize the __len__() technique in our object's class. The __len__() technique ought to return a whole number that addresses the number of elements in the object. Here is an example:

Code

Output:

5

Explanation:

In the above code, we've defined a custom object named MyObject that has a __init__() strategy to introduce a vacant list, and a __len__() technique to return the length of the list. We've then, at that point, made an occasion of the MyObject class and doled out a list of numbers to its things characteristic. At long last, we've passed the my_object case as an argument to the len() function, which returns the length of the list. The result is 5, which is the length of the list.

Conclusion:

In this article, we've found out about the len() function in Python, which is used to get the length or number of elements in an object. We've perceived how to utilize the len() function with different built-in objects like strings, lists, dictionaries, and sets, as well similarly as with custom objects. We've likewise seen the grammar of the len() function and how it works with various kinds of objects.


Next TopicPython Functions





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA