Python
Python Tutorials for Beginners
What is Python? Python is an object-oriented programming language created by Guido Rossum in 1989....
len() is a built-in function in python. You can use the len() to get the length of the given string, array, list, tuple, dictionary, etc.
You can use len function to optimize the performance of the program. The number of elements stored in the object is never calculated, so len helps provide the number of elements.
len(value)
Value: the given value you want the length of.
It will return an integer value i.e. the length of the given string, or array, or list or collections.
Various types of Return values:
It returns the number of characters in a string, which includes punctuation, space, and all type of special characters. However, you should be very careful while using len of a Null variable.
Empty is a second return call which has zero characters, but it is always None.
The len built-in returns the number of elements in a collection.
Len function depends on the type of the variable passed to it. A Non-Type does not have any built-in support.
For the dictionary, each pair is counted as one unit. However, values and keys are not independent.
# testing len()
str1 = "Welcome to gtupapers Python Tutorials"
print("The length of the string is :", len(str1))
Output:
The length of the string is : 35
# to find the length of the list
list1 = ["Tim","Charlie","Tiffany","Robert"]
print("The length of the list is", len(list1))
Output:
The length of the list is 4
# to find the length of the tuple
Tup = ('Jan','feb','march')
print("The length of the tuple is", len(Tup))
Output:
The length of the tuple is 3
# to find the length of the Dictionary
Dict = {'Tim': 18,'Charlie':12,'Tiffany':22,'Robert':25}
print("The length of the Dictionary is", len(Dict))
Output:
The length of the Dictionary is 4
# to find the length of the array
arr1 = ['Tim','Charlie','Tiffany','Robert']
print("The length of the Array is", len(arr1))
Output:
The length of the Array is 4
What is Python? Python is an object-oriented programming language created by Guido Rossum in 1989....
What is Python Enumerate? Python Enumerate() is a buit-in function available with the Python...
What is a Dictionary in Python? A Dictionary in Python is the unordered and changeable collection of...
A list is a container that contains different Python objects, which could be integers, words,...
What is Unit Testing? Unit Testing in Python is done to identify bugs early in the development stage of...
What is an Exception in Python? An exception is an error which happens at the time of execution of a...