Home »
Python
len() function with example in Python
Python len() function: Here, we are going to learn about the len() function in Python with example.
Submitted by IncludeHelp, on April 02, 2019
Python len() function
len() function is a library function in Python, it is used to get the length of an object (object may a string, list, tuple, etc). It accepts an object and returns its length (total number of characters in case of a string, the total number of elements in case of an iterable).
Syntax:
len(object)
Parameter:object – an object like string, list etc whose length to be calculated.
Return value: int – returns length of the object.
Example:
Input:
a = "Hello world!"
print(len(a))
Output:
12
Python code to get the length of an object (string, list etc)
# python code to demonstrate an example
# of len() function
a = "Hello world!" # string value
b = [10, 20, 30, 40, 50] # list
c = ["Hello", "World!", "Hi", "Friends"] # list of strings
d = ("Hello", "World!", "Hi", "Friends") # Tuple
print("length of a: ", len(a))
print("length of b: ", len(b))
print("length of d: ", len(c))
print("length of c: ", len(d))
Output
length of a: 12
length of b: 5
length of d: 4
length of c: 4
ADVERTISEMENT
ADVERTISEMENT