What does len mean in python?

In python, len() is an inbuilt function that returns the number of items(an integer value) in the iterable data structures such as string, list, tuple, etc. If there is no item present in an iterable the len() function returns 0 as a value.

The len() function internally calls the __len__() method that is is defined in the predefined classes of iterable data structures. The time complexity for the len() function is O(1).

The syntax for the len() function is given below.

#syntax:
len(seq)

Example,

string_1 = 'python'
print(len(string_1))
#output:  6

list_1 = [1, 6, "red", 'A']
print( len(list_1))
#output:  4

tuple_1 = (1, 5, 'B', 'abc', 4)
print(len(tuple_1))
#output:  5

set_1 = {3, 4, 5}
print(len(set_1))
#output:  3

dict_1 = {1: "red", 2: "black"}
print(len(dict_1))
#output:  2
If an argument passed to the len() function is of non- iterable type, the len() function returns a TypeError exception

Example,

val = 3
print("length of val: ", len(val))

Output

print("length of val: ", len(val))
TypeError: object of type 'int' has no len()
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions