Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Python program for calculating the length of string without using length() function

Calculating the length of the string without using length() function

Every datatype that stores some value have some length and length of a variable only can be calculated if its datatype can be iterated just like String can be iterated but Integer datatypes are not iterated. One way to find the length of String is by using in-built function len() and the other is by iterating through the string and incrementing the counter by one till loop ends.

Python program to calculating the length of the string without using length() function

Algorithm

  • Step 1:- Start.
  • Step 2:- Take user input.
  • Step 3:- Initialize a count variable.
  • Step 4:- Start a for loop by iterating every element of a string.
  • Step 5:- Increment count variable by 1.
  • Step 6:- Print count.
  • Step 7:- End.

Python program to find length of a string without using in-built function

#take user input
string = input('Enter the String :')
#initialize count variable
count = 0
#i is the iterator
#iterates through every element of string
for i in string:
    #increment in count variable 
    #to find number of iterators in string
    count+=1
print(count)
Output
Enter the String :PrepInsta
9