len() Python method returns the length of a list, string, dictionary, or any other iterable data format in Python. The
len() method takes one argument: an iterable object.
len() is a built-in method in Python.
When programming, you may encounter a scenario where you want to acquire the length of a particular iterable object. For example, you may have a list of orders made by customers at your car dealership, which you want to use to calculate the average car price. In order to do so, you’ll first need to know how many cars were sold.
That’s where the
len() method in Python comes in. The Python
len() method is a built-in function that can be used to calculate the length of any iterable object. So, if you want to get the length of a string, list, dictionary, or another iterable object in Python, the
len() method can be useful.
In this tutorial, we are going to explore how to use the
len() method in Python. We’ll also run through a few examples of
len() being used on strings, lists, and dictionaries.
len() function returns the number of items in an iterable object. Iterable objects such as lists and strings are indexed, which means that each value in the object is assigned an index value. This index value can be used to reference individual values in an object.
len() method takes in one argument: the iterable object of which you would like to calculate the length. Here’s the syntax of the
len() method in Python:
Let’s go through an example to illustrate how this method works. Say that you are operating a car dealership and you want to know how many cars were sold last month. You already have a list of sales, but you want to find out how many items are in that list. You could use the following code to accomplish this task:
last_month_sales = ["Vauxhall Corsa 2011", "Nissan Note 2014", "Honda Civic 2012", "Vauxhall Corsa 2015", "Vauxhall Mokka 2013", "Hyundai I20 2016"] print(len(last_month_sales))
Our code returns an integer value which represents the length of our list: 6. On the first line of our program, we declared a variable called
last_month_sales which stores all of the sales made last month at the dealership. Then, we use
len() to calculate the length of our array, and we print out that value to the console using
Python String Length
In addition, we could use
len() to get the length of a string. Let’s say that you are building a speed-typing game and you want to know how many characters a player has typed. You could use the following code to accomplish this goal:
player_one_output = "The quick brown fox jumped over the lazy dog. Indeed, he did, and it was great to witness." print(len(player_one_output))
Our code returns: 90. In Python, strings are indexed, which means each character — including spaces and symbols — is given its own value. So, the
len() function counts how many items (or characters) are in our string, which in this case is 90.
Python Len in For Loop
len() method is often used with a
for loop in Python to instruct the loop how many times to execute a particular block of code.
For example, let’s say that we want to print out a list of every car sold by our car dealership last month, and each value should be on a new line. We could do this using the following code:
last_month_sales = ["Vauxhall Corsa 2011", "Nissan Note 2014", "Honda Civic 2012", "Vauxhall Corsa 2015", "Vauxhall Mokka 2013", "Hyundai I20 2016"] for car in range(0, len(last_month_sales)): print(last_month_sales[car])
Our code uses
len() to calculate the number of cars sold last month, and then the
for loop uses this value to loop through each car in the
last_month_sales array. Here’s the output of our code:
Vauxhall Corsa 2011 Nissan Note 2014 Honda Civic 2012 Vauxhall Corsa 2015 Vauxhall Mokka 2013 Hyundai I20 2016
len() method in Python can be used to calculate the length of an iterable object. For instance,
len() could be used to calculate how many students are in a specific class at school, or how many donuts a local donut shop has sold over the last week.
In this tutorial, we discussed how to use the
len() method in Python on an iterable object. We also explored how
len() can be used with a
for loop to iterate through an iterable object. Now you have the information you need to start using Python’s built-in
len() method like an expert!
Python is used for a variety of purposes in the tech industry. Download the free Career Karma app to talk with one of our coaches about how learning Python could help you break into your dream role in tech.