The len() function is a built-in function in Python that returns the length of an object.
Here's an example of how you can use it to get the length of a string:
1 2 3 |
string = "hello" length = len(string) print(length) # Output: 5 |
This would output 5, because the string "hello" has a length of 5 characters.
You can use the len() function with other types of objects as well. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Get the length of a list lst = [1, 2, 3, 4, 5] length = len(lst) print(length) # Output: 5 # Get the length of a tuple tup = (1, 2, 3, 4, 5) length = len(tup) print(length) # Output: 5 # Get the length of a dictionary dct = {1: "one", 2: "two", 3: "three"} length = len(dct) print(length) # Output: 3 |
The len() function can be used to get the length of any object that has a defined length, such as strings, lists, tuples, and dictionaries...
Younes Derfoufi
CRMEF OUJDA
1 thought on “The Python len() function”