Table of Contents
In this tutorial, we will learn about the Python len() function with the help of examples.
The len() function returns the number of items (length) in an object.
Example
languages = ['Python', 'Java', 'JavaScript'] # compute the length of languages length = len(languages) print(length) # Output: 3
1. len() Syntax
The syntax of len() is:
len(s)
2. len() Parameters
The len() function takes a single argument s, which can be
- sequence – string, bytes, tuple, list, range OR,
- collection – dictionary, set, frozen set
3. len() Return Value
len() function returns the number of items of an object.
Failing to pass an argument or passing an invalid argument will raise a TypeError exception.
4. Example 1: How len() works with tuples, lists and range?
testList = []
print(testList, 'length is', len(testList))
testList = [1, 2, 3]
print(testList, 'length is', len(testList))
testTuple = (1, 2, 3)
print(testTuple, 'length is', len(testTuple))
testRange = range(1, 10)
print('Length of', testRange, 'is', len(testRange))
Output
[] length is 0 [1, 2, 3] length is 3 (1, 2, 3) length is 3 Length of range(1, 10) is 9
Visit these pages to learn more about:
- Python Lists
- Python Tuples
- Python range() Method
5. Example 2: How len() works with strings and bytes?
testString = ''
print('Length of', testString, 'is', len(testString))
testString = 'Python'
print('Length of', testString, 'is', len(testString))
# byte object
testByte = b'Python'
print('Length of', testByte, 'is', len(testByte))
testList = [1, 2, 3]
# converting to bytes object
testByte = bytes(testList)
print('Length of', testByte, 'is', len(testByte))
Output
Length of is 0 Length of Python is 6 Length of b'Python' is 6 Length of b'\x01\x02\x03' is 3
Visit these pages to learn more about:
6. Example 3: How len() works with dictionaries and sets?
testSet = {1, 2, 3}
print(testSet, 'length is', len(testSet))
# Empty Set
testSet = set()
print(testSet, 'length is', len(testSet))
testDict = {1: 'one', 2: 'two'}
print(testDict, 'length is', len(testDict))
testDict = {}
print(testDict, 'length is', len(testDict))
testSet = {1, 2}
# frozenSet
frozenTestSet = frozenset(testSet)
print(frozenTestSet, 'length is', len(frozenTestSet))
Output
{1, 2, 3} length is 3
set() length is 0
{1: 'one', 2: 'two'} length is 2
{} length is 0
frozenset({1, 2}) length is 2
Visit these pages to learn more about:
- Python Dictionary
- Python Set
- Python frozenset()
Internally, len() calls the object’s __len__ method. You can think of len() as:
def len(s):
return s.__len__()
So, you can assign custom length to the object (if necessary)
7. Example 4: How len() works for custom objects?
class Session:
def __init__(self, number = 0):
self.number = number
def __len__(self):
return self.number
# default length is 0
s1 = Session()
print(len(s1))
# giving custom length
s2 = Session(6)
print(len(s2))
Output
0 6
Related posts:
Python String title()
Python String translate()
Python Program to Check Whether a String is Palindrome or Not
Python Object Oriented Programming
Python Set copy()
Python Program to Count the Number of Each Vowel
Python set()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Program to Iterate Through Two Lists in Parallel
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Type Conversion and Type Casting
Python Set remove()
Python Dictionary values()
Python String isspace()
Python Program to Make a Flattened List from Nested List
Python Program to Print the Fibonacci sequence
Python Program to Randomly Select an Element From the List
Python Dictionary clear()
Python all()
Python Program to Find Hash of File
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Set intersection_update()
Python Set union()
Python Program to Capitalize the First Character of a String
Python Program to Convert Decimal to Binary Using Recursion
Python map()
Python if...else Statement
Python List count()
Python chr()
Python Tuple index()
Python Program to Find the Square Root
Python List remove()