How to use Python len: An overview

If you want to find out how many elements are in a Python object you can use the Python len function. It takes an object as a parameter and then returns the length as a result.

How to use the len function in Python

Python len is useful for finding out the number of elements in a sequence. You can also use the len function to find out the length of a Python array. This is particularly useful when you want to iterate an entire Python array. Likewise, this function is also helpful if you need to find out a Python list length. Other sequence types that Python len can be used for include Python tuples and ranges.

Additionally, the Python len function can be used for collection types. While Python dictionaries, are probably the best-known collection type, this function can also be used on Python sets (unorganized amounts) as well.

It may also surprise you to know that you can use len to find out the number of characters in a Python string. Although strings are not complex data sets, they do support the use of the function. This is due to the use of strings in programming languages. Strings are essentially nothing more than an array containing several elements of the character data type.

Tip

Using Python for your web project? IONOS Deploy Now can help you get your project online with GitHub in just three steps.

What is the syntax for Python len?

When you call the len function in Python, you provide it with a parameter. The parameter in this function is the Python object that you want to find out the length for. The returned value is an integer that represents the number of elements in the object you passed.

len(pythonobject)
python

How to find out the length of a Python list using Python len

Python len is most commonly used to find out the number of elements in a Python list. The list’s data structure is used in the programming language, which doesn’t support arrays as a replacement for them.

Note

You can also execute Python len on arrays that have been created using NumPy. When executing them, they work the same as in a list or a string.

To give you a better idea, let’s take a look at an example:

l = [0, 1, 2, 3, 4, 5, 6, 7]
len(l)
python

An example code called l has been created and contains the numbers 1 to 7. Python len is then executed on this list. As expected, the len function returns a value of 8 since there are eight values in total in the list.

How to determine the length of a string using Python len

Python len can also be used in strings to find out their length. Here’s an example of how this works:

s = "Hello World!"
len(s)
python

In the variable s, save the string “Hello World!”. You can determine the length of the string using the len function. As expected, the value returned is “12” since there are twelve characters (including spaces) in the string.

Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top