Python len Function

The len function finds the length of an object. Or, say, it returns the number of items in an 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 len function on String, Tuple, List, Dictionary, Sets, Bytes, and range. The syntax of this function is

len(Object_Name)

Python String len or length

When you use this Python 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 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

Tuple len

This example finds the length of a Tuple, which 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

List len Example

This function also helps you to find the total number of List items. First, we declared an empty List and found its length. Next, we used it to find the numeric items. Finally, it 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

Dictionary len

We can use this function on collections such as dictionaries, sets, or frozen sets. The len function on Dictionary returns the 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

set len Example

The Python len function also finds the total number of items or length in a set. Next, we declared a numeric set and string set. Using this one, 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

Length of an array

In this example, we find the size of an array. First, we calculate the length of an empty array. Next, find the total items in an Integer 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 or length Function

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

Byte len

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

by = b'Simple Example'  
print("Bytes = ", len(by)) 
Bytes =  14