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.

length of string without using strlen

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.
Calculating Length of the string without using strlen()

 

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

Run
#take user input
string = 'Hello'

count = 0

for i in string:
 
    count+=1
print(count)
5

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

7 comments on “Python Program for Calculating the length of string Without using length() function”