Python len Function

The Python len function finds the length of an object. Or, say, len function finds the total number of items in a given 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 Python len function is

len(Object_Name)

Python len Example

In this Python length example, we used this Python method to find the length of an empty string

# Python len function

lenexample = ''
print("\n", lenexample)

# Python Length Example
print("Length = ", len(lenexample))


Length =  0

Python String length

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 length of the Tutorial Gateway string

# Python len function

stringLenExample = 'Tutorial Gateway'
print("\n", stringLenExample)

# Python Length of a String Example
print("Length of a Given String = ", len(stringLenExample))

Python String Length output


 Tutorial Gateway
Length of a Given String =  16

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 empty Tuple and calculated the length of an empty tuple. Next, we are finding the length of an integer Tuple.

# Python len function

# Python Length of an Empty Tuple
tupleLen = ()
print("Length of a Tuple = ", len(tupleLen))

tupleLenExample = (10, 20, 30, 40, 50, 60, 70)
print("\n", tupleLenExample)

# Python Length of a Tuple Example
print("Length of a Tuple = ", len(tupleLenExample))

Tuple length output

Length of a Tuple =  0

 (10, 20, 30, 40, 50, 60, 70)
Length of a Tuple =  7

Python Tuple length Example 2

This time we are using len function on string tuple.

# Python len function

tupleLenExample = ('Banana', 'Orange', 'Blackberry', 'Apple')
print("\n", tupleLenExample)

# Python Length of Tuple Example
print("Length of a Tuple = ", len(tupleLenExample))

 ('Banana', 'Orange', 'Blackberry', 'Apple')
Length of a Tuple =  4

Python List Length Example

This Python len function also helps you to find the list length or find the total number of List items. First, we declared an empty List and finding its length. Next, we used the len function to find the numeric set length. Finally, fining the length of a string list.

# Python len function

# Python Length of Empty List
listLen = ()
print("Length of a List = ", len(listLen))

# Python Length of List Example 1
listLenExample = [15, 20, 35, 40, 55, 60, 70]
print("\n", listLenExample)

print("Length of a List = ", len(listLenExample))

# Python List Length Example 2
stringListLen = ['apple', 'mango', 'orange', 'cherry','banana']
print("\n", stringListLen)

print("Length of a List = ", len(stringListLen))

List Length output

Length of a List =  0

 [15, 20, 35, 40, 55, 60, 70]
Length of a List =  7

 ['apple', 'mango', 'orange', 'cherry', 'banana']
Length of a List =  5

Python Dictionary len

The Python len function on Dictionary finds the Python Dictionary length. Or, finds the total number of available items in a given dictionary.

# Python len function

# Python Length of Empty Dictionary
DictionaryLen = dict()
print("Length of a Dictionary = ", len(DictionaryLen))

# Python Length of Dictionary Example 1
DictionaryLenExample = {1: 'mango', 2: 'kiwi', 3: 'cherry'}
print("\n", DictionaryLenExample)

print("Length of a Dictionary = ", len(DictionaryLenExample))

# Python Dictionary Length Example 2
mixedDictionaryLen = {'ID': 42, 'name': 'Kevin', 'age': 25, 1: 1200}
print("\n", mixedDictionaryLen)

print("Length of a Dictionary = ", len(mixedDictionaryLen))

Dictionary Length output

Length of a Dictionary =  0

 {1: 'mango', 2: 'kiwi', 3: 'cherry'}
Length of a Dictionary =  3

 {'ID': 42, 'name': 'Kevin', 'age': 25, 1: 1200}
Length of a Dictionary =  4

Python Length of a set Example

The Python length function also finds the set length or the total number of items in a set. Next, we declared an empty set, numeric set and string set. Using this len function, we are finding the length of a numeric set, empty set and string set.

# Python len function

# Python Length of an Empty Set
SetLen = ()
print("Length of a Set = ", len(SetLen))

# Python Length of a Set Example 1
SetLenExample = {125, 200, 350, 400, 505, 607, 760}
print("\n", SetLenExample)

print("Length of a Set = ", len(SetLenExample))

# Python Set Length Example 2
stringSetLen = {'apple', 'mango', 'cherry', 'kiwi', 'orange', 'banana'}
print("\n", stringSetLen)

print("Length of a Set = ", len(stringSetLen))

Set length output

Length of a Set =  0

 {400, 760, 200, 505, 125, 350, 607}
Length of a Set =  7

 {'banana', 'kiwi', 'mango', 'orange', 'cherry', 'apple'}
Length of a Set =  6

Python length of array

In this example, we find the Python Array length. First, we are finding the length of an empty array. Next, finding the length of an Integer Array and String Array.

# Python len function

# Python Length of an Empty Array
ArrayLen = []
print("Length of a Array = ", len(ArrayLen))

# Python Length of a Array Example 1
ArrayLenExample = [5, 12, 35, 22, 40, 5]
print("\n", ArrayLenExample)

print("Length of a Array = ", len(ArrayLenExample))

# Python Array Length Example 2
stringArrayLen = ['apple', 'cherry', 'kiwi', 'mango', 'orange', 'banana']
print("\n", stringArrayLen)

print("Length of a Array = ", len(stringArrayLen))

Python Range length

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

# Python len function

# Python Length of a Range
rangeLen = range(4, 10)
print("Python Range Length = ", len(rangeLen))

# Python Length of a Range Example 2
rangeLen = range(15, 109)
print("Python Range Length = ", len(rangeLen))
Python Range Length =  6
Python Range Length =  94

Python Byte len

This Python length function also allows you to find the Byte length. This example finds the length of a byte.

# Python len function  

# Python Length of a Byte  
byteLen = b'Python Tutorial'  
print("Length of a Byte = ", len(byteLen)) 
Length of a Byte =  15