Write a Python Program to find a String Length using the built-in len function with practical examples. This programming language has a built-in function, len(), and we can use this function to find the length of the string. Apart from this, you can also use the for loop or while loop to get the result.
The Python length of the string is simply the total number of characters, which includes letters, symbols, numbers, and white spaces it contains.
Python Program to find a String Length using len()
Using a built-in common function such as len() is one of the most effective and straightforward ways to find the length. This function accepts any object that includes a list, tuple, set, or string. The program finds the string length using the len function.
In this program, we declared an empty string. Next, we find the length of a string in Python using the len function. Next, we find the total characters in the tutorial gateway and learn programming.
Remember, this len function counts empty space as 1. It means the total number of characters in ‘he ll’ is 5.
str1 = '' print("\nstr1 = ", str1) print(len(str1)) str2 = 'Tutorial Gateway' print("\nstr2 = ", str2) print(len(str2)) str3 = 'Learn Programming' print("\nstr3 = ", str3) print(len(str3))
str1 =
0
Tutorial Gateway
16
str3 = Learn Programming
17
This example starts with defining a string (str1), which contains a text that is “Tutorial Gateway”. Next, within the print statement, we use the len() function to find the length of the defined string (including space). However, you can define another variable to assign the length if you want.
Please refer to the String article to understand everything about them in Python.
str1 = "Tutorial Gateway" print("Total = ", len(str1))
Total = 16
This Python program is the same as the first example. However, we allow the users to enter their own words and then find the character count of a user given one.
str1 = input("Please enter your own String : ") print("Total Length of a Given String = ", len(str1))
Let me try with different sentences.
Python Program to find a String Length using for loop
In all the above-specified examples, we used the built-in function len. However, you can also find the length without using any built-in function. Interviews can test your coding skills.
In this program, we are using For Loop to iterate each character in a user-given sentence. Inside the Loop, we increment the val to count the characters. Next, outside the loop, we are printing the final output.
str1 = input("Please enter the text you want : ") val = 0 print("\n str1 = ", str1) for i in str1: val = val + 1 print("Total = ", val)
Please enter the text you want : Tutorial Gateway
str1 = Tutorial Gateway
Total = 16
In this example, we are using Functions to separate the logic to find the length. Next, we are calling that function to find the characters in the user-entered sentence.
def stringLength(string): length = 0 for i in string: length = length + 1 return length str1 = input("Please enter the text you want : ") print("\n str1 = ", str1) strlength = stringLength(str1) print("Characters = ", strlength)
Please enter the text you want : hello
str1 = hello
Characters = 5
Let me re-run the len program and enter a new word. It can help you understand it in more detail.
Please enter the text you want : Hello Guys
str1 = Hello Guys
Characters = 10
If you don’t want to use any built-in function, you can try either for loop or while loop. This program uses the for loop to iterate each character and increment the counter variable until it reaches the end of the string. Don’t forget to initialize the count variable to zero.
def strChars(text): count = 0 for ch in text: count += 1 return count txt = input("Please enter Text = ") print(strChars(txt))