it-Python-courses-python-string--Python-courses-python-exercises

Exercise 59

Write a python algorithm as a function which determines the length of a string without using the len () method or any other predefined method in python.

Solution

def lenStrin(s):

# initializing the length of string s
length = 0
# iterate over all characters of s
for x in s:
length = length + 1

return length

# Testing algorithm
s = "Python"

print("The length of s is : " , lenStrin(s))
# The output is : The length of s is : 6

Younes Derfoufi
my-courses.net

Leave a Reply