Python len()
The len()
function returns the number of items in an object. It is one of Python’s most commonly used built-in functions that calculates the length or size of various data types including strings, lists, tuples, dictionaries, sets, and other sequence or collection types.
The len()
function is essential for determining the size of data structures, validating input lengths, implementing loops and iterations, and performing boundary checks in algorithms. It works with any object that has a defined length, making it a versatile tool for data manipulation and analysis tasks.
Syntax
len(object)
Parameters:
object
: A sequence (such as a string, list, tuple) or collection (such as a dictionary, set) whose length is to be calculated.
Return value:
An integer value indicating the number of items in the object.
Example 1: Basic Usage
The following example demonstrates the fundamental usage of len()
with different data types:
# String lengthgreeting = "Hello, World!"string_length = len(greeting)print(f"String length: {string_length}")# List lengthfruits = ["apple", "banana", "cherry", "date"]list_length = len(fruits)print(f"List length: {list_length}")# Tuple lengthcoordinates = (10, 20, 30)tuple_length = len(coordinates)print(f"Tuple length: {tuple_length}")# Dictionary length (counts key-value pairs)student_grades = {"Alice": 95, "Bob": 87, "Charlie": 92}dict_length = len(student_grades)print(f"Dictionary length: {dict_length}")
This example results in the following output:
String length: 13List length: 4Tuple length: 3Dictionary length: 3
The len()
function counts characters in strings, elements in lists and tuples, and key-value pairs in dictionaries. Each data type returns the count of its contained items.
Example 2: Input Validation
The following example shows how to use len()
for validating user input in real-world scenarios:
def validate_username(username):"""Validate username length requirements."""min_length = 3max_length = 20username_length = len(username)if username_length < min_length:return f"Username too short. Minimum {min_length} characters required."elif username_length > max_length:return f"Username too long. Maximum {max_length} characters allowed."else:return "Username length is valid."# Test the validation functiontest_usernames = ["ab", "john_doe", "this_username_is_way_too_long_for_our_system"]for username in test_usernames:result = validate_username(username)print(f"Username '{username}' (length: {len(username)}): {result}")
This example results in the following output:
Username 'ab' (length: 2): Username too short. Minimum 3 characters required.Username 'john_doe' (length: 8): Username length is valid.Username 'this_username_is_way_too_long_for_our_system' (length: 43): Username too long. Maximum 20 characters allowed.
This demonstrates how len()
is commonly used in form validation, password requirements, and data quality checks in web applications and user interfaces.
Codebyte Example: Data Processing Pipeline
The following example illustrates using len()
in a data processing scenario to analyze and filter datasets:
This example shows how len()
is used in data analysis workflows to calculate completion rates, filter datasets, and generate statistical reports commonly found in business intelligence and research applications.
Frequently Asked Questions
1. What is the output of len([1, 2, 3])
?
The output is 3
because the list contains three elements.
2. Can len()
be used with empty objects?
Yes, len()
returns 0
for empty objects like empty strings ""
, empty lists []
, empty tuples ()
, and empty dictionaries {}
.
3. What happens if I use len()
on None?
Using len(None)
raises a TypeError
because None
does not have a length. The object must be a sequence or collection type.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours