Python String Length | len() Function, Example

In Python or other programming language, the length of a string is the total number of characters present in a string.

To find the length of a string, Python provides a built-in function named len().

This len() function returns the length of a string object or the total numbers of characters present in the string object. It counts all characters, numbers, and symbols enclosed in ” “.

Syntax of Python len() Function


The general syntax of this len() function is as follows:

len(object)

This function takes an object, such as string, tuple, set, dictionary, or custom object as a parameter.

The len() function returns an integer value denoting the length or total number of characters present in the string. That is, it returns an integer value which is the length of a string, or total number of elements present in any object like tuple, set, or dictionary.

Let’s take example programs based on the length of string using len() function.

Python String Length Example Program


Example 1:

Let’s write a program in Python to find the length of a string using len() function.

# Python program to find the length of the string.
str = "Scientech Easy"
# Call len() function to find string length.
length_of_string = len(str)
print('Length of string is',length_of_string)
Output:
      Length of string str is 14

As you can observe in the output, len() function also counts white space present between two words. The len() function returns the total length of string as 14. But, according to Python string theory, the first character of a string places at index 0, so the result must be have given as 13.

But, we have not got that. Why?

Let’s understand it with the help of an example.

In the previous example, we have stored a string value “Scientech Easy” and used the len() function. If the count starts from 0, the length of string should be 13.


In this case, the user will get confused and think that Python is giving false result because according to them the outcome must be 14.

Therefore, as a programmer or developer, we know that string index begins from 0 position but user does not know this fact. Therefore, while performing the length operation on string, we assume the index position is starting from 1, not 0.


Note: We do not use dot operator (.) to access the len() function.


Example 2:

Let’s write a program to find the number of characters present in the string using len() function.

language = "Python Programming"
# Call len() function to find string length.
length_of_string = len(language)
print(f'Number of characters in the string "{language}" is',length_of_string)
Output:
      Number of characters in the string "Python Programming" is 18

If you pass the value of bool or int type in the len() function, it will throw TypeError. For example:

print(len(20))
print(len(True))
Output:
      TypeError: object of type 'int' has no len()
      TypeError: object of type 'bool' has no len()

How to find Length of String in Python without using len() Function?


Example 3:

Let’s write a program in Python to calculate the length of string without using built-in len() function.

# Program to find the length of a string without using len() function.
# Create a function.
def main():
    user_str = input('Enter a string: ')
    count_char = 0
    for ch in user_str:
        count_char += 1
    print(f"Length of entered string is '{count_char}'")

main() # calling function.
Output:
      Enter a string: Every people love his country in the world.
      Length of entered string is '43'

In this example, we have assigned the entered string into a variable named user_str and zero value to variable count_char. The variable count_char wroks like a counter which keeps the track of number of characters and gets incremented by a value 1 when the user_str is iterated using for loop.

Find Length of List, Tuple, Set, and Directory


Example 4:

Let’s write a program in Python to calculate the length of list, tuple, set, and dictionary using len() function. That is, we will calculate the total number of elements present in the list, tuple, etc.

num_list = [10, 20, 30, 40, 50, 60]
length_list = len(num_list)
print('Total number of elements in list is',length_list)

name_tuple = ('john', 'harry', 'bob', 'mark')
length_tuple = len(name_tuple)
print('Total number of elements in tuple is',length_tuple)

city_set = {'Dhanbad', 'Godda', 'Sydney', 'New York'}
length_set = len(city_set)
print('Total number of elements in set is',length_set)

dic = {1: 'one', 2: 'two', 3: 'three'}
length_dic = len(dic)
print('Number of elements in dic is',length_dic)
Output:
      Total number of elements in list is 6
      Total number of elements in tuple is 4
      Total number of elements in set is 4
      Number of elements in dic is 3

In this tutorial, you have learned about how to find length of string in Python using len() function. Hope that you will have understood the basic points of len() function and practiced all example programs.
Thanks for reading!!!

⇐ PrevNext ⇒

Please share your love