Python String Length

Write a Program to find Python String Length. It is one of the frequently asked questions. The python programming language provides a building-in standard function called len. It can help you to find the length of a string in Python or any other object type.

Python String Length Example

In this string length program, we declared an empty string. Next, we find the string length using the len function. Next, we are finding the length of the tutorial gateway and python programming. Remember, Python len function counts empty space as 1. It means the length of he ll is 5.

# Python String Length Example

str1 = ''
print("\n Original String str1 = ", str1)
print("Length of a String str1 = ", len(str1))

str2 = 'Tutorial Gateway'
print("\n Original String str2 = ", str2)
print("Length of a String str2 = ", len(str2))

str3 = 'Python Programming'
print("\n Original String str3 = ", str3)
print("Length of a String str3 = ", len(str3))

 Original String str1 =  
Length of a String str1 =  0

 Original String str2 =  Tutorial Gateway
Length of a String str2 =  16

 Original String str3 =  Python Programming
Length of a String str3 =  18

String Length Example 2

This program is the same as the first string length example. However, we allow the users to enter their own string and then find the length of a user given string.

# String Length Example

str1 = input("Please enter the string you want : ")

print("\n Original String str1 = ", str1)

print("The length of a Given string = ", len(str1))
Please enter the string you want : Python Programming Language

 Original String str1 =  Python Programming Language
The length of a Given string =  27

Let me try with different string

Python String Length 3

Python String Length Example 3

In all the above-specified string length examples, we used the built-in function len. However, you can also find string length without using any built-in function. In interviews, this can test your coding skills.

In this Python string length program, we are using For Loop to iterate each character in a user given string. Inside the Loop, we increment the length to count the characters in a string. Next, outside the loop, we are printing the final length as an output.

Please refer String, For Loop and len function articles in Python.

# Python String Length Example

str1 = input("Please enter the string you want : ")
length = 0

print("\n Original String str1 = ", str1)

for i in str1:
    length = length + 1

print("The length of a Given string = ", length)
Please enter the string you want : Tutorial Gateway

 Original String str1 =  Tutorial Gateway
The length of a Given string =  16

String Length Example 4

In this python string length example, we are using Functions to separate the logic to find the length of a string. Next, we are calling that function to find the length in a user-entered string.

# Python String Length Example

def stringLength(string):
    length = 0
    for i in string:
        length = length + 1
    return length
        
str1 = input("Please enter the string you want : ")

print("\n Original String str1 = ", str1)

strlength = stringLength(str1)

print("The length of a Given string = ", strlength)
Please enter the string you want : hello

 Original String str1 =  hello
The length of a Given string =  5

Let me re-run the string len program and enter a new word. It can help you understand the String Length function in more detail.

Please enter the string you want : Python Tutorial

 Original String str1 =  Python Tutorial
The length of a Given string =  15