DEV Community

DDSRY
DDSRY

Posted on • Edited on

What does the len() function do in Python ?

The len() function do in PythonProgramming Language:

▪ It's give the length of a string (in number) which you have stored into a variable.

Top comments (2)

Collapse
 
amal profile image
Amal Shaji

len returns the length of an iterator or the __len__ method of a class if defined

>>> a = [1, 2, 3, 4]
>>> len(a)
4
>>> a = "amalshaji"
>>> len(a)
9
>>> a = (1, 2, 3, 4)
>>> len(a)
4
>>> a = {1, 2, 3, 4}
>>> len(a)
4
>>> class Amal:
...     def __init__(self):
...             pass
...     def __len__(self):
...             return 5
...
>>> a = Amal()
>>> len(a)
5
Collapse
 
ddsry21 profile image
DDSRY

Awesome, Thank you for sharing this.