How to find the length of an object in Python

Python is a high level computer programming language which comes with many already builtin tools that help the coder to accomplish almost any task, they need to. Being an object oriented computer programming language, some of the Python objects have a length, which can be easily accessed with the help of the Python builtin utility called len().

Python objects that have a length are strings, tuples, lists and dictionaries. One can easily use the Python builtin function len() to find the length of an object which supports one.

According to the official Python documentation, the Python builtin function len(), stands for the length of an object in Python computer programming language. As I have mentioned in earlier tutorials related to Python, the Python computer programming language is easy to read, like a human language.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

The following is the syntax which can be used to utilize the Python builtin function len(), with the main purpose of finding the length of an object.

len(Python object which supports length in here)

With the main purpose of illustrating the usage of the Python builtin len() utility in a practical example, I am creating the following string.

website = ‘codetheory.in’

To find the length of the above Python string, the Python builtin len() utility can be easily utilized like shown below.

len(website)

When the above piece of code is being run in the Python shell, in interactive mode, the following is being displayed on the console. It is the number of the characters found in the string ‘codetheory.in’.

13

As mentioned earlier in this article, Python objects such as tuples, lists, and dictionaries have a length too. To give other practical examples of the Python len() utility in action, I am building the following objects.

t = (1, 2, 3)
l = [1, 2, 3, 4, 5]
d = {‘profession’: ‘coder’}

In the above Python code, there are three objects being declared: a tuple, a list and a dictionary. One can easily find the length of each one of the above Python objects by using the builtin len() utility in Python computer programming language; like shown below.

length_of_tuple = len(t)
length_of_list = len(l)
length_of_dictionary = len(d)

To print the length of each of the Python objects declared about, make use of the Python print statement like shown below.

print(length_of_tuple)
print(length_of_list)
print(length_of_dictionary)

Final thoughts

The Python computer programming language comes with many already builtin tools which can ease the work of the coder and speed up the process of development. One of them is the len() function, which can be used to find the length of a Python object which supports one.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *