The len() function in Python is used to get the length or the number of items in a sequence such as a string, list, tuple, dictionary, or set.
For example, if we have a list of numbers, we can use the len() function to get the number of elements in the list:
numbers = [1, 2, 3, 4, 5] print(len(numbers)) # Output: 5
Similarly, if we have a string, we can use the len() function to get the number of characters in the string:
string = "Hello, World!" print(len(string)) # Output: 13
The len() function can also be used with other built-in Python data structures like tuples, dictionaries, and sets to get their lengths.
In summary, the len() function is a useful built-in Python function for getting the length or the number of items in a sequence.
0 Comments