► Python program to find length of a string without using len function


Steps followed:
1. take input from user and store in var variable
2. use for loop to count all characters in var
3. print result 


Program

#python program to find length of a string without using len function

#getting input from user and storing in var variable
var=input("Enter a string: ")
length=0

# using for loop to count all characters in var
for i in var:
    length+=1

#print result
print("length of the input string - '%s' is %d"%(var,length))
 


Output

Enter a string: Mercury is the closest planet to the Sun
length of the input string - 'Mercury is the closest planet to the Sun' is 40





Also Read: