How to Get Python String Length

To get the length of a string in Python use the len() function. Pass the string to evaluate as the first argument of the function and the actual length of the string will be returned as an integer (not the index count.)

 

To demonstrate this, let's test out len() on different strings and see the result.

 

string_1 = 'hello'
string_2 = 'hello john'
string_3 = ''

print(len(string_1))
print(len(string_2))
print(len(string_3))
print(type(len(string_3)))
5
10
0
<class 'int'>

 

As you can see from the example above, if an empty string is supplied to len() it will return 0.