In this Python program, we will learn how to get the length of a given string. We will find the length of the string using the loop and by using a python built-in function len()
.
Here is the source code of the program to get the length of a given string in Python.
# Python program to calculate the length of a String without using len() function
# To take the input from the user
mystr = input("Enter a string: ")
# counter variable to count the character in a string
counter = 0
for s in mystr:
counter = counter+1
print("Length of the input string is:", counter)
Enter a string: Tutorialsrack.com is an online learning website
Length of the input string is: 47
Enter a string: Program 1: Python program to calculate length of a String without using len() function
Length of the input string is: 86
Program to find the length of string using built-in function len()
# Program to find the length of string using library function
# To take the input from the user
mystr = input("Enter a string: ")
# using len() function to find length of str
print("Length of the input string is:", len(mystr))
Enter a string: Program 1: Python program to calculate length of a String without using len() function
Length of the input string is: 86
Enter a string: counter variable to count the character in a string
Length of the input string is: 51
Comments