How To Use Len Python

In this tutorial, you will learn about the Len Python function and how to use it effectively.

The len() function is a built-in Python function that returns the length or the number of items in an object such as a list, array, or string. It is a widely used function and is incredibly useful for various programming tasks.

Step 1 – Understanding the len() function

The len() function provides a concise way to determine the size of a data structure, such as a list or string, and is used in Python’s built-in functions. len() function can be used to get the length of a string, list, tuple, dictionary, or any other iterable.

The general syntax of the len() function is as follows:

Here, the object is the data structure or iterable whose length has to be calculated.

Step 2 – Using len() with strings

To find the length of a string, simply pass the string variable or the string itself as an argument to the function. The function will return the number of characters in the string, including spaces and special characters.



Step 3 – Using len() with lists

To find the length of a list, pass the list variable to the len() function. The function will return the number of elements present in the list.



Step 4 – Using len() with dictionaries

You can also use len() to find the number of key-value pairs in a dictionary. Pass the dictionary variable to the function and it will return the count of key-value pairs.



Step 5 – Using len() with other data structures

In addition to strings, lists, and dictionaries, you can use the len() function with other data structures, such as sets and tuples.



Full Code


The len() function in Python provides an easy and efficient method to determine the size or number of elements in different data structures like strings, lists, dictionaries, sets, and tuples. It is incredibly useful and essential for various programming tasks. By using the len() function, you can perform operations that are dependent on the sizes of your data structures with simplicity and ease.