1. What Is the len() Function in Python?
The len() function is one of the most frequently used built-in functions in Python. It helps determine the length of an object — meaning the total number of items it contains. This could be the number of characters in a string, the number of elements in a list or tuple, or even the number of keys in a dictionary. In simple words, len() tells you how big or long your data structure is. It plays a major role in tasks like loops, validation, data analysis, and input processing.
2. Syntax of the isupper() Method
Below is the synax of isspace() Method.
len(s)
The syntax is short and intuitive — you pass an object s, and Python returns its length.
3. Parameter Description
| Parameter | Type | Description |
|---|---|---|
| s | Sequence or collection | Any object whose length can be measured — examples: string, list, tuple, dictionary, set, range, and more. |
Python internally uses the object’s built-in __len__() method to compute the result.
Return Value
- Returns an integer representing the number of items present in the object.
- For strings → number of characters
- For lists/tuples/sets → number of elements
- For dictionaries → number of key-value pairs
5. Examples of Using the len() Function in Python
Below are practical examples that show how the isupper() method behaves with different types of strings.
Example 1: Length of a String
text = "Hello, World!"
print(len(text)) # Output: 13
Explanation:
The string contains 13 characters, including letters, punctuation, and spaces.
Example 2: Length of a List
numbers = [10, 20, 30, 40, 50]
print(len(numbers)) # Output: 5
Explanation:
The list has 5 elements, so len() returns 5.
Example 3: Length of an Empty List
empty_list = []
print(len(empty_list)) # Output: 0
Explanation:
Since the list has no elements, its length is 0.
Example 4: Length of a Tuple
items = ('a', 'b', 'c', 'd')
print(len(items)) # Output: 4
Explanation:
The tuple contains 4 items, so the result is 4.
Example 5: Length of a Dictionary
person = {'name': 'Alice', 'age': 30, 'city': 'New York'}
print(len(person)) # Output: 3
Explanation:
A dictionary’s length is the number of key-value pairs. Here, there are 3.
Example 6: Length of a Set
unique_numbers = {1, 2, 3, 4, 4, 2}
print(len(unique_numbers)) # Output: 4
Explanation:
Sets automatically remove duplicates. Even though duplicates appear, only 4 unique values remain.
Example 7: Length of a Nested List
nested = [1, 2, [3, 4], 5]
print(len(nested)) # Output: 4
Explanation:
The outer list has 4 elements.
The nested list [3,4] counts as one single element, not two.
Example 8: Length of a Unicode String
unicode_str = "café"
print(len(unicode_str)) # Output: 4
Explanation:
Python treats “é” as one character, so the full string length is 4.
Example 9: Using len() on a Generator or Iterator (TypeError)
gen = (i for i in range(5))
# print(len(gen)) # Raises TypeError: object of type 'generator' has no len()
Explanation:
Generators and iterators don’t store items in memory, so Python cannot determine their length. Attempting to use len() raises a TypeError.