Python len Function

The Python len function finds the length of an object. Or, say, it returns the number of items in a object. If you are finding the string length, then this Python len function returns the total number of characters.

In this section, we discuss how to use this Python len function on String, Tuple, List, Dictionary, Sets, Bytes, and range. The syntax of this len function is

len(Object_Name)

Python length of a String

When you use this Python len function on a String object, it counts and returns the total number of characters (including spaces) in a string. Or, say, finds the length of a string in Python. In this example, we are finding the size of an empty string and Tutorial Gateway.

a = ''
print(a)
print(len(a))

b = 'Tutorial Gateway'
print(len(b))


0

6

Python length of a Tuple

This example finds the length of a Tuple using Python len function. It means counting the total number of items in this tuple. First, we declared an integer and the string Tuple. Next, it returns the number of items in an integer and string Tuple.

tupleExample = (10, 20, 30, 40, 50, 60, 70, 80, 90)
print(len(tupleExample))

tupleExample2 = ('Banana', 'Orange', 'Blackberry', 'Apple')
print(len(tupleExample2))
9

4

Python Length of a List Example

This function also helps you to find the find the total number of List items. First, we declared an empty List and finding its length. Next, we used it to find the numeric items. Finally, calculates the number of elements in a string list.

listLe = ()
print(len(listLe))

listLeExample = [15, 20, 35, 40, 55, 60, 70]
print(len(listLeExample))

stringListLe = ['apple', 'mango', 'orange', 'cherry','banana']
print(len(stringListLe))
0

7

5

Python Dictionary len

We can use this function on collections such as dictionary, sets, or frozen sets. The Python len function on Dictionary returns the Python Dictionary length. Or, calculates the total number of available items in a given dictionary.

DictionaryLe = dict()
print(len(DictionaryLe))
print()

DictionaryLeExample = {1: 'mango', 2: 'kiwi', 3: 'cherry'}
print(len(DictionaryLeExample))
print()

mixedDictionaryLe = {'ID': 42, 'name': 'Kevin', 'age': 25, 1: 1200}
print(len(mixedDictionaryLe))
0

3

4

Python Length of a set Example

It also finds the total number of items in a set. Next, we declared a numeric set and string set. Using this function, we are returning the elements of a numeric set, and string set.

SetExample = {125, 200, 350, 400, 505, 607, 760}
print(len(SetExample))

print()
stringSet = {'apple', 'mango', 'cherry', 'kiwi', 'orange', 'banana'}
print(len(stringSet))
7

6

Python length of array

In this example, we find the size of an array. First, we are calculating the length of an empty array. Next, finding the total items. in an Integer Array and String Array.

Arr = []
print(len(Arr))

ArrExample = [5, 12, 35, 22, 40, 5]
print("\n", ArrExample)

print(len(ArrExample))

stringArr = ['apple', 'cherry', 'kiwi', 'mango', 'orange', 'banana']
print("\n", stringArr)

print(len(stringArr))
Python len Function

Length of a range

The len function is not only about returning object size, but you can also find the range length. In this example, we declared a range from 4 to 10 and returns the total elements between that range. To understand better, we used another example with a big range.

rangeLe = range(4, 10)
print(len(rangeLe))

rangeLe = range(15, 109)
print(len(rangeLe))
6
94

Python len Byte

This function also allows you to find the Byte length and this example returns the same.

by = b'Python Tutorial'  
print("Bytes = ", len(by)) 
Bytes =  15