Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

Python Len, String Length Examples and Performance

This Python 3 article explores the len method on strings. Len returns the length of a string, dictionary, list, set or tuple.

Len. All things have lengths. Even our universe, in constant expansion, has a length.

In Python though we mostly measure the lengths of strings, lists, collections—not universes.

We use len. Often we can optimize performance with len. The number of elements is stored on the object, not calculated, so len is fast.

Strings. Len returns the number of characters in a string. It counts spaces, punctuation, all characters the same. We must be careful with taking the len of a None variable—this fails.

Empty: The second len call tests an empty string. This string has zero characters but is not None.

TypeError: Len relies on the type of the variable passed to it. A NoneType has no len built-in support.


Based on:

Python 3

Python program that uses len on strings

# Has length of 3:
value = "cat"

# Has length of 0:
value = ""

# Causes TypeError:
value = None


Traceback (most recent call last):
  File "C:\programs\", line 13, in <module>
TypeError: object of type 'NoneType' has no len()

Collections. The len built-in returns the number of elements in a collection. For a collection with nested, sub-collections, counting is shallow: not all nested elements are considered.

Dictionary: For the dictionary, each pair is counted as one unit. Keys and values are not independent.


Python program that uses len, collections

# Get length of list with len.
elements = [1, 2, 3]

# Get length of tuple.
items = ("cat", "dog", "bird", "shark")

# Length of example set (key count).
set = {100, 200, 300}

# Length of dictionary (pair count).
lookup = {"cat" : 4, "centipede" : 100}



Nested lists. Let us revisit nested collections. A collection itself is an element, so it counts just one time. The len built-in does not recurse. It does not even loop. It is simple.


So: We must do those things (recurse, loop) with custom code. We can test sub-elements and use len on them.


Python program that uses len on nested list

# A nested list:
list = [1, 2, [4, 5]]

# Shallow count of elements.



Error. We cannot just take the len of any variable. This program attempts to take the length of an int variable. And it fails, miserably, with a TypeError that ends its operation.

Note: Conceptually len() counts countable units: chars in a string, elements in a list. A number has digits, but no other "units."


Python program that causes error, len on int

value = 100

# Cannot take length of int:
length = len(value)


Traceback (most recent call last):
  File "C:\programs\", line 6, in <module>
    length = len(value)
TypeError: object of type 'int' has no len()

Performance. The len of collections and strings is stored as a number in memory. It is not computed, as in a loop, each time it is accessed. For this reason, len is much faster than a loop.

Here: I access the length of a string with len in a loop. This is timed. I then test a for-loop version.

Result: It is many times faster to access len. The for-loop is useful only when counting chars, where their values matter.

Python program that times len, char counting

import time

value = "characters"


# Version 1: len
for i in range(0, 1000000):
    length = len(value)
    if length != 10:
	raise Exception()


# Version 2: count chars
for i in range(0, 1000000):
    length = 0
    for c in value:
	length += 1
    if length != 10:
	raise Exception()



1406752804.606887    len      = 0.281 s
1406752806.05097     for-loop = 1.444 s

A summary. A length cannot be negative. So we can use len as a loop boundary: this is a convenient way to loop over a list. But when not needed, avoiding len is ideal.

Loop suggestion. Consider a for-in loop to avoid using len. This loop construct will enumerate each element in a collection. No indexes are needed.