Python len() Method Tutorial

Python provides the len() method in order to return numbers of the items in a list, dictionary, string, tuple, etc. len() method name comes from the term length which is popular in programming languages.

len() Method Syntax

len() mehtod is a builtin method which is provided by default. So there is no need to import a module or call from an object. We will just provide the list, dictionary, string or tuple to count their items or characters accordingly.

len(OBJECT)
  • As a simple method the len() accept single parameter OBJECT which can be a list, tuple, string etc.
  • Return value will be an integer which is the length of the given object.

len() Method Examples

As stated previously len() method can be used over different object or variable types like list, tuple, string etc.

l = [ 1 , 2 , 3 , "ismail" , "ahmet" ]

t = ( 1 , 2 , 3 , "ismail" , "ahmet" )

s = "pythontect"

d = { "apple":3 , "banana":12 , "potato":2 }


l_len = len(l)
print(l_len)

t_len = len(t)
print(t_len)

s_len = len(s)
print(s_len)

d_len = len(d)
print(d_len)

Count Items in List with len() Method

We can use the len() method in order to count the items in a list. We will create multiple lists that contains numbers, strings or nested lists to count.

l1 = [ 1 , 2 , 3 , "ismail" , "ahmet" ]


l2 = [ 1 , 2 , 3 ]


l3 = [ 1 , 2 , 3 , [ 1 , 2 , 3 ]
 ]


l1_len = len(l1)
print(l1_len)

l2_len = len(l2)
print(l2_len)

l3_len = len(l3)
print(l3_len)

Count Items in Dictionary with len() Method

Dictinaries consiste of items which are key-value pairs. These dctionary item counts can be displayed with the len() method.

d1 = { 1:1 , 2:2 , 3:3 , "ismail":1 , "ahmet":2 }


d2 = { 1:1 , 2:2 , 3:3 }


d3 = { 1:1 , 2:2 , 3:3 , 4:{ 1:1 , 2:2 , 3:3 } }


d1_len = len(d1)
print(d1_len)

d2_len = len(d2)
print(d2_len)

d3_len = len(d3)
print(d3_len)

Count Items in Set with len() Method

Set data type is an implementation of themathematical set. That means a set can not store the same value or item multiple time. We can use the len() method in order to count set items too.

s1 = { 1 , 2 , 3 , "ismail" , "ahmet" }


s2 = { 1 , 2 , 3 }


s1_len = len(s1)
print(s1_len)

s2_len = len(s2)
print(s2_len)

Count Items in Set with len() Method

Count Items in Tuple with len() Method

Tuple is another data type where items count can be returned with the len() method like below.

t1 = ( 1 , 2 , 3 , "ismail" , "ahmet" )


t2 = ( 1 , 2 , 3 )


t3 = ( 1 , 2 , 3 , ( 1 , 2 , 3 )
 )


t1_len = len(t1)
print(t1_len)

t2_len = len(t2)
print(t2_len)

t3_len = len(t3)
print(t3_len)

Count Characters in String with len() Method

Strings are consist of one or more characters. These characters can be a letter, number or sign even a space. len() method can be used to count these characters and return character count of the given string.

s1 = "pythontect.com"

s2 = "ahmet"

s3 = "ali"


s1_len = len(s1)
print(s1_len)

s2_len = len(s2)
print(s2_len)

s3_len = len(s3)
print(s3_len)

Leave a Comment