Python len() function
The len() function in Python returns the number of items in an object.
Syntax
1 2 3 |
len(s) |
Parameter
s: This parameter represents a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).
Return
This function returns the length (the number of items) of an object.
Example 1
1 2 3 4 5 6 7 |
# Python Program explaining # the len() function inp_list = ["Monday", "Tuesday", "Wednesday"] len_val = len(inp_list) print("Total length: ",len_val) |
Output
1 2 3 |
Total length: 3 |
Example 2
1 2 3 4 5 6 7 8 |
# Python Program explaining # the len() function int_str = "example" print(len(int_str)) int_str = "tutorials and example" print(len(int_str)) |
Output
1 2 3 4 |
7 21 |