Python is a versatile, high-level programming language that finds extensive application across various domains such as data science, web development, and automation. It offers numerous built-in functions that enhance the ease and speed of programming. A particularly useful function among these is len, which is utilized to determine the length of an object.
The Python function len serves to obtain the length (i.e., the count of items) of a given object. This function accepts a single argument, which can be any object that allows for length evaluation, and it outputs the total number of elements contained within that object. The argument may be a string, list, tuple, set, word reference, or any other type of iterable object.
Python len Function Syntax
It has the following syntax:
len(object)
Parameters
object: An entity that is required to be a sequence (such as a string, tuple, list, etc.) or a collection (including a dictionary, set, etc.)
Return
It provides the size (the count of elements) of an object.
Different Examples for Python len Function
In this section, we will explore a variety of examples illustrating the use of the Python len function.
Python len Function Example 1
The following example demonstrates how to determine the length of a string. In this instance, we will employ the len function to retrieve the length of a given string.
# using len() with a string
string = "Hi, World!"
length = len( string )
print( length )
Output:
Explanation:
In the code provided, a string variable referred to as string has been established, holding the value "Hi, World!". Subsequently, this string variable is utilized as an argument for the len function, which computes the total number of characters present within the string. The output is 10, representing the length of the string.
Python len Function Example 2
The following example demonstrates how to determine the length of a list. In this instance, we will employ the len function to obtain the length of a specified list.
# using len() with a list
my_list = [ 1, 2, 3, 4, 5 ]
length = len( my_list )
print( length )
Output:
Explanation:
In the code provided, we have established a list variable called my_list that includes the integers from 1 to 5. Subsequently, we have passed this list variable to the len function, which calculates and returns the total count of elements present in the list. The output is 5, indicating the length of the list.
Python len Function Example 3
The following example demonstrates how to determine the length of a dictionary. In this instance, we will employ the len function to obtain the total count of key-value pairs present within a dictionary.
# using len() with a dictionary
my_dict = { " name " : " John ", " age " : 30, " gender " : " male "}
length = len( my_dict )
print( length )
Output:
Explanation:
In the code presented above, a dictionary variable referred to as my_dict has been established, consisting of several key-value pairs. Subsequently, this dictionary variable is supplied as an argument to the len function, which calculates and returns the total count of key-value pairs within the dictionary. The outcome of this operation is 3, representing the length of the dictionary.
Python len Function Example 4
The following example illustrates how to determine the length of a set. In this instance, we will employ the len function to count the total number of items within a set.
# using len() with a set
my_set = {1, 2, 3, 4, 5}
length = len(my_set)
print(length)
Output:
Explanation:
In the code presented above, we have created a variable called my_set that holds the integers from 1 to 5. Subsequently, this set variable is provided as an argument to the len function, which calculates and returns the total count of elements contained within the set. The outcome is 5, representing the length of the set.
Python len Function Example 5
We can employ the len function with our user-defined objects. To achieve this, we need to define the len method within our object's class. The len method should return an integer that signifies the total number of items contained in the object. Below is an illustration:
# using len() with a custom object
class MyObject:
def __init__(self):
self.items = []
def __len__(self):
return len(self.items)
my_object = MyObject()
my_object.items = [1, 2, 3, 4, 5]
length = len(my_object)
print(length)
Output:
Explanation:
In the code presented above, a custom object called MyObject has been established, featuring an init method designed to initialize an empty list, along with a len method that provides the length of this list. Subsequently, we instantiated an object of the MyObject class and assigned a collection of numbers to its items attribute. Finally, we supplied the my_object instance as an argument to the len function, which yields the length of the list. The outcome is 5, representing the total number of elements in the list.
Conclusion
In this article, we have explored the len function in Python, which serves the purpose of determining the length or the count of elements within an object. We have examined how to apply the len function across various built-in objects such as strings, lists, dictionaries, and sets, as well as with user-defined objects. Additionally, we have looked into the syntax of the len function and its functionality with different types of objects.