How to Find the Length of Strings, Lists, and Dictionaries in Python
Measuring the length of objects is a fundamental task in Python, whether you're working with strings, lists, or dictionaries. Python's built-in len()
function makes it easy to get the number of characters, elements, or key-value pairs in these data types. This article explains how to use len()
with strings, lists, and dictionaries, with syntax, code examples, outputs, and best practices.
Table of Content
What is len() in Python?
- Definition:
len()
is a built-in Python function that returns the number of items in an object. - Use Cases: Works with strings, lists, dictionaries, tuples, sets, and more.
- Benefits: Simple, efficient, and works consistently across many data types.
References: [3][6][8]
01. Find the Length of a String
To get the number of characters in a string (including spaces, punctuation, and special characters), use len()
with the string as its argument.
text = "Hello, World!"
print(len(text))
Output:
13
len(text)
returns the number of characters in the string, including spaces and punctuation.- Works with empty strings (returns 0) and any Unicode characters.
References: [1][2][3][4][5][6][7][8]
02. Find the Length of a List
To find the number of elements in a list, pass the list to len()
.
fruits = ["apple", "banana", "cherry"]
print(len(fruits))
Output:
3
len(fruits)
returns the count of elements in the list.- Works for lists of any data type, including mixed types and nested lists.
References: [2][3][6][8]
03. Find the Length of a Dictionary
For dictionaries, len()
returns the number of key-value pairs.
person = {"name": "Alice", "age": 30, "city": "New York"}
print(len(person))
Output:
3
len(person)
counts the number of key-value pairs in the dictionary.- Empty dictionaries return 0.
References: [6][8]
04. len() with Other Data Types
len()
also works with tuples, sets, and other collections:
numbers = (1, 2, 3, 4)
print(len(numbers)) # Tuple
unique_items = {10, 20, 30}
print(len(unique_items)) # Set
Output:
4
3
- Returns the number of items in tuples and sets, just like for lists.
- For unsupported types (like integers or booleans),
len()
raises aTypeError
.
References: [2][3][6][8]
05. Manual Length Calculation (for loop)
You can also calculate length manually by iterating and counting items. This is educational but less efficient than using len()
.
my_list = [10, 20, 30, 40]
count = 0
for item in my_list:
count += 1
print(count)
Output:
4
- Increments a counter for each item in the collection.
- Helps understand how
len()
works internally.
References: [7]
06. Comparing len() Usage in Python
Type | What len() Returns | Example | Notes |
---|---|---|---|
String | Number of characters | len("hello") → 5 | Includes spaces, punctuation |
List | Number of elements | len([1, 2, 3]) → 3 | Works for any list |
Dictionary | Number of key-value pairs | len({"a": 1, "b": 2}) → 2 | Keys must be unique |
Tuple | Number of elements | len((1, 2, 3)) → 3 | Immutable sequence |
Set | Number of unique elements | len({1, 2, 3}) → 3 | Unordered, unique items |
Conclusion
Python's len()
function is the standard way to find the length of strings, lists, dictionaries, and other collections. It is simple, reliable, and works across many data types. Use len()
whenever you need to measure the size of a collection or string in your Python programs.
len()
for length checks, and remember it works differently depending on the data type!
Comments
Post a Comment