Base case: Empty string or single character returns itself.
Recursive case: reverse(s[1:]) + s[0]
def reverse_string(s):
if len(s) <= 1:
return s
return reverse_string(s[1:]) + s[0]
Tracing reverse_string("abc"):
reverse_string("abc") = reverse_string("bc") + "a"
reverse_string("bc") = reverse_string("c") + "b"
reverse_string("c") = "c" <- Base case
Working back up: "c" -> "cb" -> "cba". Each call takes the first character, recurses on the rest, then appends the first character at the end.