Member-only story
Interview Question: Get length of string without using the len function in Python
Any individual who wants to work as a Python developer is very likely to need to go on an interview for the position. It is for that reason that Python enthusiasts need to work on as many prospective interview questions as they can in order to be prepared for any question that an interviewer may give them to answer.
One interview question that I came across was to find the length of a string without using Python’s len function.
The code below is something that I developed and would like to pass on to those individuals who read my blog posts.
The pseudocode for the solution to this interview question is as follows:-
- Define the variable as a string, string.
- Defined a second variable as an empty string, new_str.
- Define the variable, i, and initialise it to 0.
- Create a while loop that will work as long as string does not equal new_str. Add the position character position of i to the string and then increment i by 1.
- When the while loop has completed its iterations, print out new_string, which should be the same as str.
- Print out the value of i, which should be the length of new_str.