len()

The built-in len() function returns the length of an object, which can either be a sequence or collection.

Syntax

len(object)

An object passed to the len() function is commonly one of the following:

Example

The example below demonstrates how the len() function is used in a Python program:

print(len("Hello, World!"))
# Output: 13

Codebyte Example

In the example below, the len() function is used to return the length of a string, dictionary, and list:

Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback form.

Learn Python on Codecademy