Python’s built-in `len()` gives you the number of characters in a string or items in a list. It also works with tuples and other collections. Learn how it differs from `count()`, which tracks how often a specific value appears—a small distinction that can save confusion.

Multiple Choice

What function is used to get the length of a string or list?

The function used to obtain the length of a string or list in Python is `len()`. This built-in function takes one argument, which can be a string, list, tuple, or other iterable, and returns the number of items or the number of characters in the case of a string. For example, if you have a string `s = "Hello"` and you call `len(s)`, it will return `5`, indicating that there are five characters in the string. Similarly, if you have a list `lst = [1, 2, 3, 4]`, calling `len(lst)` will return `4`, as there are four elements in the list. Other options like `length()`, `size()`, and `count()` are not valid functions for this purpose in Python. `length()` is not defined in Python; `size()` is often found in other programming languages but not for obtaining the length of strings or lists in Python, and `count()` is a method that counts the occurrence of a specific value in a list or string but does not return the length.

Python gives you a simple way to find out how many items a string or list contains: use the built-in len() function. It’s short, easy to remember, and works with many common data types. If you’ve ever needed to check how many characters are in a name or how many values are in a collection, len() is the tool for the job.

Meet len(): one function, many uses

Call len() with a single object inside the parentheses:


len(object)

The function returns an integer: the number of items in that object. For a string, those items are characters. For a list, they’re elements.


greeting = "Hello"

print(len(greeting))

The output is:


5

And with a list:


colors = ["blue", "green", "red"]

print(len(colors))

This prints 3, because the list holds three elements. The values don’t have to be numbers or even all the same type:


things = ["notebook", 12, True]

print(len(things))

The result is also 3. Python counts the elements in the list, not the number of letters, digits, or types inside them.

That distinction is useful. A list is like a row of labeled boxes: len() counts the boxes, not everything packed inside each one.

What does Python count as a character?

For a regular string, len() counts the string’s characters. An empty string has no characters, so its length is zero:


message = ""

print(len(message))

Output:


0

Spaces count, too. They may look like empty gaps, but Python stores them as part of the string:


message = "Hi there"

print(len(message))

The result is 8, including the space between the words. This often matters when handling user input, checking a word limit, or measuring a piece of text.

There’s one small detail worth knowing: Python counts Unicode code points, which usually match what people think of as characters, but not always. Some symbols that appear as a single visible character are made from multiple code points. An emoji with a skin-tone modifier, for example, may count as more than one unit with len(). For everyday strings, the result is usually just what you’d expect. For fine-grained text processing, though, visual characters and Python’s count can differ.

Lists, nested lists, and empty collections

A list can hold other lists. In that case, len() counts only the items at the outer level:


grid = [[1, 2, 3], [4, 5, 6]]

print(len(grid))

This prints 2, not 6. There are two elements in grid, and each element happens to be another list. To count the items in one of those inner lists, call len() on it directly:


print(len(grid[0]))

That prints 3.

This is a common point of confusion because a nested list can look like one big group of values. But Python treats each inner list as a single element of the outer list. If you need a total across several levels, you’ll need to write code that visits each inner list and adds up its length.

The same rule applies to an empty list:


empty_list = []

print(len(empty_list))

The result is 0. There’s no special error or placeholder value; an empty collection simply has no items.

len() works beyond strings and lists

Although strings and lists are familiar examples, len() also works with several other built-in types. Tuples, dictionaries, sets, and ranges all have a length:


coordinates = (4, 9)

print(len(coordinates))  # 2

scores = {"Ava": 88, "Noah": 91}

print(len(scores))  # 2

unique_numbers = {2, 4, 6, 8}

print(len(unique_numbers))  # 4

A dictionary’s length is the number of key-value pairs, not the number of individual keys and values added together. In the example, there are two pairs, so the length is 2.

A range has a length based on how many numbers it represents:


numbers = range(5)

print(len(numbers))

This gives 5, since the range contains the values 0 through 4. The ending number itself isn’t included, a detail that often surprises people when they first work with ranges.

Why not count()?

Python also has a count() method, but it answers a different question. It tells you how many times a particular value appears in a string, list, or tuple.


letters = ["a", "b", "a", "c", "a"]

print(letters.count("a"))

The output is 3, because "a" appears three times. But the list has five elements, so:


print(len(letters))

prints 5.

For strings, the same distinction applies:


word = "banana"

print(len(word))       # 6

print(word.count("a")) # 3

Think of len() as asking, “How many items are here?” count() asks, “How many times does this particular value show up?” Similar-looking questions, different answers.

Why length() and size() aren’t the usual tools

Python doesn’t provide a built-in function called length() for strings and lists. It’s an understandable name—some other languages use it—but Python uses len() instead.

Likewise, size() isn’t the standard built-in way to get the number of elements in a Python list or the characters in a string. You may encounter methods or properties with names such as size in certain libraries, but for ordinary Python collections, reach for len().

The function’s name is short because it’s used so often. It’s also part of a wider Python design pattern: common operations, such as getting an object’s length, are available through built-in functions rather than requiring a separate method on each type.

A useful habit: check before you act

Knowing an object’s length can help a program make sensible decisions. For instance, a function might need to handle an empty list before trying to read its first item:


items = []

if len(items) == 0:

print("There are no items yet.")

For an empty collection, checking its length can prevent an error caused by trying to access an item that isn’t there. In many cases, Python also lets you test whether a collection is empty directly:


if not items:

print("There are no items yet.")

Both styles are common. The first makes the length check explicit; the second is compact and idiomatic. Choose the form that makes the code easiest to understand in context.

You can also store the result if you’ll use it more than once:


names = ["Mina", "Leo", "Sam"]

number_of_names = len(names)

print(number_of_names)

Here, number_of_names holds the integer 3. The original list stays unchanged. len() measures the collection; it doesn’t add, remove, or alter anything.

A small function with a big role

At its heart, len() has a simple job: it returns the number of items in a supported object. Use it for the number of characters in a string, the elements in a list, the pairs in a dictionary, or the items in many other built-in collections.

Keep the distinction between length and occurrence in mind: len() counts everything in the collection, while count() looks for a particular value. And when you’re working with a string or list in Python, you don’t need to guess at a special method name. The familiar call is:


len(my_string)

len(my_list)

Small function, clear answer. That’s a nice bit of Python.