AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Python len: how to find the length of your object

PARTAGEZ

If you want to know how many elements a Python object contains, you can use the Python function len. It takes an object as a parameter and returns its length to you as a return value.

Scope of the len function

You can resort to the Python len function whenever you want to know the number of elements in a sequence type. For example, you can use the len function to determine the length of a Python array. This is particularly useful if you want to iterate over the entire Python array. The same is true if you need the length of a Python list. Other sequence types you can apply Python len to are Python tuples and the range function.

The Python len function can also be applied to collection types. The most illustrious representatives of collections are probably Python dictionaries. But Python sets (unordered sets) are also included.

This may surprise you, but you can also use len to find out the number of characters in a Python string. Although strings are not considered complex data structures in Python, they still support the application of the len function. This is due to the implementation of strings in programming languages: strings are basically nothing more than an array multiple elements of the character data type.

Using Python to program a web project? IONOS also supports you in your web projects with Deploy Now. Get your project online in three simple steps with GitHub and IONOS.

Python syntax len

The len function accepts a parameter when called. This is the Python object whose length you are interested in. The return value of the function is of type integer. She returns the number of elements contained in the passed object.

Length of a list

The Python len function is most often used to find out the number of elements in a Python list. In the programming language that does not support arrays natively, the list data structure is often used to replace a Python array.

You can also call Python len on arrays you have created using the numpy library. The function behaves the same as a call with a list or string when called.

We can rely on a code example to illustrate the operation of the Python List len ​​function:

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

python

First, a list named “l” is created that includes the numbers 0 through 7 in the sample code. Python len is then called on this exact list. As you might expect, then returns the integer value “8” in this case, because eight values ​​were saved in the list.

Length of a chain

Python len can also be applied to strings, and in this case returns the length of the string. Here too, a code example illustrates the application:

s = "Hello World!"
len(s)

python

In the variable s, you store the string “Hello World!” ‘, the length of which you then determine by calling the len function. As expected, the integer value “11” is returned here, because the string is eleven characters long (including spaces).

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact