14-2. Function Polymorphism

Output:

6
10
3

Actually, while the len() function can indeed be used to return the number of characters in a string or the number of elements in a list, its behavior isn't limited to just those data types. The len() function is a built-in Python function that can be applied to any object that supports the concept of "length" or "size".

The len() function is designed to return the length (number of items) of an object. The specific way it calculates the length depends on the type of the object it's operating on. This ability to handle different types of objects is what makes len() polymorphic.

The len() function is natively supported for several built-in types:

  • Strings: Returns the number of characters in the string.
  • Lists: Returns the number of elements in the list.
  • Tuples: Returns the number of elements in the tuple.
  • Dictionaries: Returns the number of key-value pairs.
  • Sets: Returns the number of elements in the set.