THE WORLD'S LARGEST WEB DEVELOPER SITE

Python len() Function

❮ Built-in Functions


Example

Return the number of items in a list:

mylist = ["apple", "banana", "cherry"]
x = len(mylist)
Run example »

Definition and Usage

The len() function returns the number of items in an object.

When the object is a string, the len() function returns the number of characters in the string.


Syntax

len(object)

Parameter Values

Parameter Description
object Required. An object. Must be a sequence or a collection

More Examples

Example

Return the number of characters in a string:

mylist = "Hello"
x = len(mylist)
Run example »

❮ Built-in Functions