In Python, len()
is a built-in function that returns the number of items in an object. The object can be a string, list, tuple, dictionary, or any other iterable object. The len()
function is a very useful tool for programmers as it helps to determine the size or length of an object. In this article, we will explore the len()
function in detail with code examples.
Syntax of len() Function
The syntax of the len()
function is straightforward. It takes one argument, which is the object whose length or size we want to determine. The syntax is as follows:
len(object)
Here, object
is the argument that we pass to the len()
function. It can be any iterable object like a string, list, tuple, dictionary, etc.
Examples
Let’s look at some examples of using the len()
function to understand it better.
Example 1: Finding the Length of a String
name = "John Doe"
length = len(name)
print("The length of the string is:", length)
# Output: The length of the string is: 8
In this example, we have a string variable name
that contains the value “John Doe”. We pass this string to the len()
function, which returns the length of the string as 8. We then print the length using the print()
function.
Example 2: Finding the Length of a List
fruits = ["apple", "banana", "cherry"]
length = len(fruits)
print("The length of the list is:", length)
# Output: The length of the list is: 3
In this example, we have a list variable fruits
that contains three items. We pass this list to the len()
function, which returns the length of the list as 3. We then print the length using the print()
function.
Example 3: Finding the Length of a Tuple
numbers = (1, 2, 3, 4, 5)
length = len(numbers)
print("The length of the tuple is:", length)
# Output: The length of the tuple is: 5
In this example, we have a tuple variable numbers
that contains five items. We pass this tuple to the len()
function, which returns the length of the tuple as 5. We then print the length using the print()
function.
Example 4: Finding the Length of a Dictionary
person = {"name": "John", "age": 30, "city": "New York"}
length = len(person)
print("The length of the dictionary is:", length)
# Output: The length of the dictionary is: 3
In this example, we have a dictionary variable person
that contains three key-value pairs. We pass this dictionary to the len()
function, which returns the length of the dictionary as 3. We then print the length using the print()
function.
Example 5: Finding the Length of an Empty Object
empty_list = []
length = len(empty_list)
print("The length of the empty list is:", length)
# Output: The length of the empty list is: 0
In this example, we have an empty list variable empty_list
. We pass this list to the len()
function, which returns the length of the list as 0 because it has no items. We then print the length using the print()
function.
Example 6: Finding the Length of a Set
unique_numbers = {1, 2, 3, 4, 5}
length = len(unique_numbers)
print("The length of the set is:", length)
# Output: The length of the set is: 5
In this example, we have a set variable unique_numbers
that contains five unique items. We pass this set to the len()
function, which returns the length of the set as 5. We then print the length using the print()
function.
Conclusion
In this article, we have explored the len()
function in Python, which is used to determine the length or size of an object. We looked at the syntax of the len()
function and provided several examples of its usage with different types of objects. The len()
function is a powerful tool for programmers, and it can help to make their code more efficient and effective.