In this program, we will take a string as input and calculate the length of a string with loop logic
Steps to solve the proram
- Take a string as input and create a variable and assign its value equal to zero.
- Use For loop to iterate over each character of the string.
- After each iteration add 1 to the variable to find the length of the string.
- To verify our solution find the length of the string using built it function len().
- Print the output.
#Input string
string = "im am learing python"
count = 0
for char in string:
count +=1
#Printing output
print("Length of the string using loop logic: ", count)
print("Length of the string using len(): ", len(string))
Output :
Length of the string using loop logic: 20
Length of the string using len(): 20