I saw this used in code and I didn't understand how it worked. Does it save what it chops off somewhere?
here is an example (from askpython):
def rev_string(x):
if len(x) == 0:
return x
else:
return rev_string(x[1:]) + x[0]
x = "STRING"
print ("Original string: ")
print (x)
print ("Reversed string: ")
print (rev_string(x))
it seems like it chops off the first character each recursion, which disappears until almost the end when it repeats the "+ x[0]" part all at once for all of the letters.
How does it work?