Menu Close

Python len() Function

python len function

In this article, you will learn everything about the Python len() function to get the length of the python object. The len() function in Python is mostly used by Python programmers. It is a built function in Python used to find the length of any Python object.

In previous tutorials, we have seen the Python iter() function to return the iterator object.

Python len() function

Python len() function is a built-in function in python that comes with Python by default. len() function is used to find the length of the Python object.

Syntax

The syntax of len function in python is:-

len(object)

Parameter

len function in Python accepts one parameter that will be a Python object, string, list, tuple, set, dictionary, etc.

Return Value

The return value of len function in python is the length of the Python object.

Python len() function examples

In this section, I am going to give you a complete test of the len() function to find the length of different-different Python objects.

Example: Getting the length of a Python string

# length of the string
print(len('Programming Funda'))

The Output will be:- 17

Example: Getting the length of the Python list


# length of the list
print(len([1, 2, 3, 4, 5, 6, 7, 8]))

The Output will be:- 8

Example: Finding the length of a Python Dictionary


student = {
    "name": "Vishvajit",
    "age": 22,
    "occupation": "Developer"
}
print("Length of dictionary is:- ", len(student))

Output

Length of dictionary is:-  3

Example: Getting the length of a Python tuple


tple = ('Python', 'Java', "C++")
print("Length of tuple is:- ", len(tple))

Output

Length of tuple is:-  3

Example: Getting the length of the Python set


set = {'Python', 'Java', "C++", 'Dot Net', 'PHP'}
print("Length of set is:- ", len(set))

Output

Length of set is:-  5

Conclusion

In this article, you have learned all about the Python len() function to get the length of any kind of Python object like a string, list, tuple, dictionary, etc.
len() is a very helpful function when we are working on any real-life projects because in that case, need to take decisions on the basis of the length of particular Python objects. you.

If you like this article, please share and keep visiting for further python built-in functions tutorials.

You can explore more about Python built-in functions from here.

Python built-in functions


For more information:- Click Here

Thanks for your valuable time…❤️❤️❤️

Python oct() Function
Python int() Function

Related Posts