How to Use len() Function in PythonPython Basics


The len() function returns the length of an object (such as list) which is the number of items in the object. When the object is a string, the len() function returns the number of characters in the string.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

temp1 = "Hello"
print("len(hello):  ", len(temp1))

temp2 = [1,2,3,4,5]
print("len([1,2,3,4,5]):  ", len(temp2))
Output:

len(world):   5
len([1,2,3,4,5]):   5

Syntax

len(x)

Parameters

x — Object.

Return Value

It returns the length of an object.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us