len()
The built-in len()
function returns the number of items in an object, such as sequences or collections. It’s useful for determining the size of data structures:
>>> len([1, 2, 3, 4])
4
len()
Signature
len(obj, /)
Arguments
Argument | Description |
---|---|
obj |
An object that supports the length |
Return Value
- Returns an integer representing the number of items in the input object.
len()
Examples
With a string as an argument:
>>> len("Hello")
5
With lists and tuples as an argument:
>>> len([1, 2, 3, 4])
4
>>> len(("John", "Python Dev", "Canada"))
3
With dictionaries and sets as an argument:
>>> len({"name": "Jane", "age": 25})
2
>>> len({"red", "green", "blue"})
3
len()
Common Use Cases
The most common use cases for the len()
function include:
- Determining the number of elements in a list, tuple, or set.
- Counting the number of characters in a string.
- Calculating the number of key-value pairs in a dictionary.
- Explicitly checking whether a container is empty by comparing
len()
to0
.
len()
Real-World Example
You can use len()
to validate user input, such as ensuring a username is within a certain length:
username.py
username = input("Enter a username: ")
if 4 <= len(username) <= 12:
print("Username is valid.")
else:
print("Username must be between 4 and 12 characters.")
In this example, you use len()
to check whether the entered username meets the required length criteria, ensuring valid input.
len()
in Custom Classes
You can support len()
in your custom classes by implementing the .__len__()
special method. Here’s an example:
box.py
class Box:
def __init__(self, items):
self.items = items
def __len__(self):
return len(self.items)
# Usage
box = Box([1, 2, 3, 4, 5])
print(len(box)) # Output: 5
By implementing .__len__()
, you enable your custom class to work seamlessly with the len()
function, providing a way to determine the number of items it contains.
Related Resources
Tutorial
Using the len() Function in Python
In this tutorial, you'll learn how and when to use the len() Python function. You'll also learn how to customize your class definitions so that objects of a user-defined class can be used as arguments in len().
For additional information on related topics, take a look at the following resources:
- Python's list Data Type: A Deep Dive With Examples (Tutorial)
- Python's tuple Data Type: A Deep Dive With Examples (Tutorial)
- Lists vs Tuples in Python (Tutorial)
- Python Sequences: A Comprehensive Guide (Tutorial)
- Object-Oriented Programming (OOP) in Python (Tutorial)
- Python Classes: The Power of Object-Oriented Programming (Tutorial)
- Python's Magic Methods: Leverage Their Power in Your Classes (Tutorial)
- Python's len() Function (Course)
- Lists and Tuples in Python (Course)
- Lists vs Tuples in Python (Quiz)
- Python Sequences: A Comprehensive Guide (Quiz)
- Intro to Object-Oriented Programming (OOP) in Python (Course)
- Object-Oriented Programming (OOP) in Python (Quiz)
- Class Concepts: Object-Oriented Programming in Python (Course)
- Inheritance and Internals: Object-Oriented Programming in Python (Course)
- Python Classes - The Power of Object-Oriented Programming (Quiz)
- Python's Magic Methods in Classes (Course)
- Python's Magic Methods: Leverage Their Power in Your Classes (Quiz)