Python len() Function

Python len() Function

The len() function in Python is super convenient and one that you will use all the time when making programs. The input to len() is either a sequence or a collection and it returns the length when the function is called. You can use len() to get the length of anything you want in Python. For example, it can be used with strings, lists, tuples, and dictionaries. Another good use case for using len() is in combination with loops or the range() function. Let’s look at several ways to use the len() function now.


Using len() with strings

Starting off quite simply, we can use len() to check the length of a string. Here are several examples of doing just that.

0

1

3

31

6

11

4

The length of your name is: 7

6

len() of string practical example

In the examples below, we store a sequence of numbers as a string to represent zip codes. We can use len() to determine if the zip codes are of the correct length. Since all zip codes need to be 5 characters in length, we can use len() to determine if a given zip code is valid or invalid.

Valid

Invalid

Valid

Using len() with lists

The len() function is super useful when working with lists in Python. There are countless examples of when you need to know the length of a list in both simple and complex programs. Here are several examples of using len() with a list.

3

3

4

2

5

len() of lists practical examples

What is dog name number 1? ("q" to quit):
Moe
What is dog name number 2? ("q" to quit):
Buddy
What is dog name number 3? ("q" to quit):
Winnie
What is dog name number 4? ("q" to quit):
q
You entered 3 dog names.
Here they are for you:
 Moe
 Buddy
 Winnie

Using len() in a loop

In the example below, we have a list of yard tools. We can loop over the names of each yard tool, and print out the length of each one using the len() function.

Snow blower has 11 characters
Tractor has 7 characters
Lawn Mower has 10 characters

We can do a similar thing with the Simpsons, and f-strings.

Homer has a total of 5 characters.
Marge has a total of 5 characters.
Bart has a total of 4 characters.
Lisa has a total of 4 characters.
Maggie has a total of 6 characters.

len() in a list comprehension

The len() function also works well with list comprehensions in Python.

[6, 4, 7]

Using len() with range()

This example shows how to use the len() function with the Python range() function. This is a useful approach because the code in the loop can access the variable i index as well as the value at the index. The nice thing about using len() with range() is that no matter how few or how many items are in the list, the code iterates over them accurately with no need for counters.

Index 0 in gifts is: Gaming PC
Index 1 in gifts is: Ceiling Fan
Index 2 in gifts is: Nintendo Watch

Note as we change the size of the list here, the code still handles it correctly.

Index 0 in gifts is: Gaming PC
Index 1 in gifts is: Ceiling Fan
Index 2 in gifts is: Nintendo Watch
Index 3 in gifts is: More Gifts!
Index 4 in gifts is: Even More Gifts!!

Counting command line args with len()

This neat use of the len() function lets us count the number of command-line arguments that have been passed to a python file. We can also count the total length of all command-line arguments.

PS C:\Python> python testing.py 1 2
You passed 2 arguments to the testing.py file.
The total length of all command-line arguments is 2

PS C:\Python> python testing.py 'hi' 'ho' 'silver'
You passed 3 arguments to the testing.py file.
The total length of all command-line arguments is 10

len() with tuples, dictionaries, and sets

You can also use the len() function with the tuple, dictionary, and set data types. Here are a few examples of that.

5

4

3

Python len() Function Summary

We’ve seen plenty of examples of how to use the len() function in different ways. You’ll find yourself using this handy function all the time in your Python programs.