Python Tutorial Python Advanced Python References Python Libraries

Python - String Length



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 example below, 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