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.
Syntax of len() function
The syntax of len() funtion is
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().
mystring = 'python examples' #length of string length = len(mystring) print('Length of the string is:', length)
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.
mystring = '' length = len(mystring) print('length of the is:', length)
length of the is: 0
In this tutorial of Python Examples, we learned how to find the length of a string with the help of well detailed examples.