How to get Python String Length?

Python String Length

To find the length of the string, use len() function with this string passed as argument. The len() function returns an integer representing the length of the given string.

In this tutorial, we will learn how to find the length of a given string, in different scenarios, using example Python programs.

Python String Length

Syntax of len() function

The syntax of len() funtion is

len(mystring)

where mystring is the string whose length has to be found out.

Example 1: Find String Length in Python

In this example, we will take a string constant and find its length using len().

Python Program

mystring = 'python examples'

#length of string
length = len(mystring)

print('Length of the string is:', length)
Run

Output

Length of the string is: 15

The number of characters, including all alphabets, spaces, and any characters, is 15 in the given string.

Example 2: Find Empty String Length

If you try to find out the length of an empty string, it returns zero.

Python Program

mystring = ''

length = len(mystring)
print('length of the is:', length)
Run

Output

length of the is: 0

Summary

In this tutorial of Python Examples, we learned how to find the length of a string with the help of well detailed examples.