SoFunction
Updated on 2024-11-13

A brief introduction to the use of the len function in Python

Function: len()

1: Role:Returns the length of a string, list, dictionary, tuple, etc.

2: Grammar:len(str)

3: Parameters:
str: string, list, dictionary, tuple, etc. to be calculated

4: Return Value: length of elements of strings, lists, dictionaries, tuples, etc.

5: Examples
5.1. Calculate the length of the string:

>>> s = "hello good boy doiido"
>>> len(s)
21


5.2. Calculate the number of elements of a list:

>>> l = ['h','e','l','l','o']
>>> len(l)
5


5.3. Calculate the total length of the dictionary (i.e., the total number of key-value pairs):

>>> d = {'num':123,'name':"doiido"}
>>> len(d)
2


5.4. Calculate the number of tuple elements:

>>> t = ('G','o','o','d')
>>> len(t)
4