The len() function can be used with various data types in Python, and it returns the following:
-
Strings: Returns the number of characters in the string.
len("Hello") # Output: 5 -
Lists: Returns the number of elements in the list.
len([1, 2, 3]) # Output: 3 -
Tuples: Returns the number of elements in the tuple.
len((1, 2, 3)) # Output: 3 -
Dictionaries: Returns the number of key-value pairs in the dictionary.
len({'a': 1, 'b': 2}) # Output: 2 -
Sets: Returns the number of elements in the set.
len({1, 2, 3}) # Output: 3 -
Other collections: Any object that implements the
__len__()method can be used withlen().
In general, len() provides a way to determine the size or count of elements in various data structures.
