How to get Python String Length? Examples


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.

https://youtu.be/JhvdSeABNys?si=8Rep6SGee-L4vmNQ

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.

Examples

1. Find length of given string

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)

Output

Length of the string is: 15

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

2. Find length of empty string

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)

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.