Facebook Page Twitter Page LinkedIn Page
× Python Tutorial Python Advanced Python Resources Python Libraries


The Python len() function is used to find out the total number of characters in the string. Apart from Alphabets, any symbol, space, special character or number in the string is also considered as a character while finding out the length of the string. To add more to this, len() function returns the number of elements in an iterable. An iterable object can be any data structure like list, tuple, set, string and dictionary, etc.

Syntax

len(str)

Parameters

str Required. specify iterable like a string to find out total number of characters in it.

Return Value

Returns total number of characters in the string.

Example:

In the below example, len() function is used on the string to find out the total number of characters in it.

MyString = "Learn Python"
#Returns total number of characters in the string.
x = len(MyString)    
print(x)

The output of the above code will be:

12

❮ Python String Methods