How To Use The Len Function In Python

3 min read

The len function in Python is one of those built-in tools you will use in almost every program you write, whether you are checking the size of a list, validating a password, counting the words in a text file, or debugging a data pipeline. This guide walks through how to use len() correctly across strings, lists, dictionaries, sets, custom objects, and more — plus the subtle mistakes that trip up beginners and the performance details that matter once your code grows.

What Is the len() Function in Python?

len() is a built-in function that returns the number of items contained in an object. The name is short for length, and the value it returns is always a non-negative integer.

The key idea to understand early: len() does not count characters, bytes, or memory. For a dictionary, they are keys. For a string, those items are characters. For a list, they are elements. In practice, it counts items in a container. This distinction explains most of the confusing behavior people run into later Took long enough..

Short version: it depends. Long version — keep reading.

The Basic Syntax of len()

The syntax is deliberately simple:

len(object)
  • object — any object that supports the sized protocol.

And the result:

numbers = [10, 20, 30, 40]
print(len(numbers))   # 4

print(len("Python"))  # 6

Notice that len() is a function, not a method. len(). That said, you write len(my_list), not my_list. This is an intentional design choice in Python: the language keeps a small set of common operations as built-ins so they work uniformly across every type that supports them.

It sounds simple, but the gap is usually here.

Using len() with Strings

With strings, len() returns the number of Unicode code points, which for everyday text is the same as the number of characters you see.

greeting = "Hello, world!"
print(len(greeting))  # 13

Spaces and punctuation count. An empty string returns 0:

print(len(""))  # 0

Counting Characters vs. Counting Bytes

This is where beginners get surprised. A character and a byte are not the same thing once you leave plain ASCII Practical, not theoretical..

word = "café"
print(len(word))                    # 4 characters
print(len(word.encode("utf-8")))    # 5 bytes

The accented é is one character but two bytes in UTF-8. Similarly, an emoji like "👍" has len() of 1, while a complex emoji built from several joined code points (such as a family emoji or a flag) can return 5, 7, or more Simple, but easy to overlook..

The practical rule: use len() when you care about user-visible characters, and use len(text.encode("utf-8")) when you care about storage size or network payload.

Using len() with Lists, Tuples, and Other Sequences

len() works identically on every sequence type:

fruits = ["apple", "banana", "cherry"]
print(len(fruits))         # 3

coords = (4, 5)
print(len(coords))         # 2

empty = []
print(len(empty))          # 0

Two behaviors are worth memorizing:

  1. Nested items count as one item each. len([[1, 2], [3, 4]]) returns 2, not 4. To go deeper, index first: len(matrix[0]).
  2. Duplicates are counted. len([1, 1, 1]) returns 3. If you want unique values, convert to a set first: len(set([1, 1, 1])) returns 1.

Using len() with Dictionaries and Sets

For dictionaries, len() counts keys, not values:

profile = {"name": "Ada", "age": 36, "city": "London"}
print(len(profile))  # 3

Adding a key increases the count; overwriting an existing key does not:

profile["age"] = 37
print(len(profile))  # still 3
Out Now

New Writing

Similar Territory

One More Before You Go

Thank you for reading about How To Use The Len Function In Python. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home