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

pythonlen(object)

Example

Let's find out the length of the string:

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

Result of code execution:

python3

Example

Let's find out the length of the list:

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

Result of code execution:

python3

Example

Let's find out the length of the dictionary:

pythondct = {
  'a': 1,
  'b': 2,
  'c': 3
}

print(len(dct))

Result of code execution:

python3

Example

Let's find out the length of the set:

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

Result of code execution:

python3

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