Contents
hide
In this tutorial, We will see the len() Function Python.
len() Function Python
len() is one of the built-in functions in python. The len() function can be used to find the length of python string, python list, python tuple, python dictionary, python set, etc.
Syntax of Python len() Function
len(value)
- len() function return the total number of element in the value.
- Here, value can be string, list, tuple, set, dictionary, etc.
Example: How To Find Length of String in Python?
#Example 1 txt = "Python is a programming language" #String txt1 = "Hello, How are you?" #String print(len(txt)) #Output: 32 print(len(txt1)) #Output: 19
Example: How To Find Length of List in Python?
#Example 2 list1 = [1,2,3,4,5] #List list2 = ["Python","Java","Javascript"] #List print(len(list1)) #Output: 5 print(len(list2)) #Output: 3
Example: How To Find Length of a Tuple in Python?
#Example 3 tup1 = (1,2,3,4,5) #tuple tup2 = ("Python","Java","Javascript") #tuple print(len(tup1)) #Output: 5 print(len(tup2)) #Output: 3
Example: How To Find Length of a Set in Python?
#Example 4 set1 = {1,2,3,4,5} #set set2 = {"Python","Java","Javascript"} #set print(len(set1)) #Output: 5 print(len(set2)) #Output: 3
Example: How To Find Length of a Dictionary in Python?
#Example 5 dict = {"country1":"USA","country2":"China","country3":"India"} #country1,country2,country3 are the keys. #USA, China, India are the values. #Total Number of items is 3. print(len(dict)) #Output: 3
Check your knowledge: Python Quizzes