def reverse(text):
final_string = ""
count = len(text)
while count > 0:
final_string += text[len(text)-1]
text = text[0:len(text)-1]
count -= 1
return final_string
This is the code snippet. I know it reverses the string "text" but can't seem to understand how it does so.
textand places them at the end offinal_string. But why don't you throw someprints in there and verify this yourself?text = text[0:len(text)-1]-> reduces the text everytime by 1 less character from the right. i.e. iftext = "Prakhar",text = text[0:len(text)-1]makes it Prakha. The next iteration will make it Prakh.