Python len() of String: How It Works & Examples

Formulas & functions · Updated November 22, 2026

The len() function in Python returns the number of characters in a string. For example, len("hello") returns 5. This built-in function works on any sequence or collection, but for strings it counts the Unicode code points, which is usually what you mean by “length”.

What len() Actually Counts

For a string, len() counts the number of Unicode code points. A code point is essentially a single character as defined by the Unicode standard. In most everyday cases, this matches the number of characters you see on screen. For instance:

len("Python")  # 6
len("héllo")    # 5
len("😀")       # 1

Notice that the emoji 😀 is a single code point, so len() returns 1. However, some emojis are composed of multiple code points. For example, the family emoji 👨‍👩‍👧‍👦 is actually a sequence of four emoji characters joined by zero-width joiners, so len() returns 7. This is a common source of confusion.

How len() Works Under the Hood

Unlike some languages where you might loop through a string to count characters, Python’s len() is O(1) — it runs in constant time. That’s because strings in Python store their length as a separate attribute internally. When you call len(), it simply reads that stored value, not iterate through the string.

This design means that even very large strings (millions of characters) return their length instantly. It also means that len() is consistent and fast, which is why it’s used everywhere in Python code.

Using len() with Strings in Real Code

The most common use is checking if a string is empty or has a minimum length. For example:

username = input("Enter username: ")
if len(username) < 3:
    print("Username must be at least 3 characters.")

You can also use len() to slice a string:

text = "Python programming"
last_3 = text[-3:]  # uses len() implicitly
print(last_3)  # "ing"

And in loops, len() is often used to iterate by index:

for i in range(len(text)):
    print(i, text[i])

But note that Python’s for loop can iterate directly over characters, which is more readable and efficient:

for ch in text:
    print(ch)

Common Pitfalls and Misconceptions

  • len() counts Unicode code points, not bytes. If you need the number of bytes (e.g., for network transmission), use len(text.encode('utf-8')).
  • len() on a string with combining characters. For example, the letter “é” can be represented as a single code point (U+00E9) or as “e” plus a combining accent (U+0301). len() returns 1 or 2 respectively, even though they look the same.
  • len() returns 0 for an empty string. That’s correct — an empty string has no characters.
  • len() is not a method. It’s a built-in function. You write len(s), not s.len(). This trips up beginners coming from other languages.

Comparing len() with Other Languages

If you’re coming from another language, you might be used to str.length() in Java or .Length in C#. Python’s len() is a function, not a method, and it works on many types: strings, lists, tuples, dictionaries, sets, and more. This uniformity is a Python design principle — you don’t have to remember different method names for each type.

In JavaScript, you’d use str.length (a property). In C, you’d use strlen() which counts bytes and is O(n). Python’s approach is both simpler and faster for repeated calls.

Performance and Memory Considerations

Because len() is O(1), you can call it millions of times without performance issues. However, be aware that each string object stores its length, which adds a small memory overhead per string. This is negligible for most applications.

If you’re working with very large strings and need to check emptiness, prefer if not s: over if len(s) == 0:. The former is more Pythonic and slightly faster because it checks the truthiness of the string directly (an empty string is falsy).

Alternatives to len() for Specific Needs

  • Number of bytes: len(s.encode('utf-8'))
  • Number of words: len(s.split())
  • Number of lines: s.count('\n') + 1 (if the string ends with a newline, adjust accordingly)
  • Number of occurrences of a substring: s.count('a')
  • Number of characters excluding spaces: len(s.replace(' ', ''))

Frequently Asked Questions

Does len() count spaces?

Yes, spaces are characters like any other. len("a b") returns 3 because it counts the space.

Does len() count newline characters?

Yes, a newline (\n) is a single character. For example, len("a\nb") returns 3.

Why does len() give a different result for emojis?

Some emojis are made of multiple code points. For example, the rainbow flag emoji 🏳️‍🌈 is actually four code points: a white flag, a variation selector, a zero-width joiner, and the rainbow. So len() returns 4. To count visible characters, you’d need to use a library like grapheme.

Can I use len() on a string with accented characters?

Yes, but the result depends on how the accent is encoded. If the accent is a separate combining character, it counts separately. For example, len("e\u0301") returns 2, while len("é") (the precomposed form) returns 1.

Is len() available in all Python versions?

Yes, len() has been a built-in function since Python 1.0 and works the same in Python 2 and Python 3 for strings (though Python 2 strings are bytes by default).

What is the maximum length of a string in Python?

There is no fixed maximum, but it’s limited by available memory. In CPython, the length is stored as a Py_ssize_t, which on 64-bit systems can hold numbers up to 9,223,372,036,854,775,807 — far beyond practical memory limits.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.