In this example, we will show you how to build a recursive function that reverses an input string. Let’s start coding.
def reverse_string(my_string): if len(my_string) == 0: return my_string else: return reverse_string(my_string[1:]) + my_string[0]
We will test the function by entering the string abcde
.
reverse_string('abcde')
'edcba'
Good job!