len

Full name
len
Library
Built-in
Syntax

len(s)

Description

The len function returns the length (the number of elements) of an object. The argument can be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

Parameters
  • s: Object whose elements we want to count.
Result

The len function returns an integer.

Examples

This first example counts the number of characters in a text string:

print(len("Python"))

Len function. Example of use

 

It can also be used to count the number of items in a dictionary:

d = dict([("one", 1), ("two", 2)])
print(len(d))

Len function. Example of use

 

In this example the function returns the number of elements in a list:

m = ["a", "b", 1, 2, "c"]
print(len(m))

Len function. Example of use

 

Submitted by admin on Sun, 01/20/2019 - 14:55