106 of 151 menu

The len function

The len function is the length of an iterable object in Python: it can be a string, list, dictionary, set, tuple. In the function parameter, we pass the object whose length we want to know.

Syntax

len(object)

Example

Let's find out the length of the string:

txt = 'abc' print(len(txt))

Result of code execution:

3

Example

Let's find out the length of the list:

lst = ['ab', 'cd', 'ef'] print(len(lst))

Result of code execution:

3

Example

Let's find out the length of the dictionary:

dct = { 'a': 1, 'b': 2, 'c': 3 } print(len(dct))

Result of code execution:

3

Example

Let's find out the length of the set:

st = {'a', 'b', 'c'} print(len(st))

Result of code execution:

3

See also

  • method clear,
    which deletes all elements of the dictionary
  • method copy,
    which copies a dictionary
  • method update,
    which updates a dictionary with the keys and values ​​of another dictionary
byenru