Challenge: Reverse Recursively
Completed
Read a line of text and print it backwards using a recursive function (no slicing with [::-1], no reversed).
Input
One line of text (at most 300 characters).
Output
The text reversed.
Example
Input
stressed
Output
desserts
Editor
Python
Hints
Base case: a string of length 0 or 1 is its own reverse.
reverse(s) = reverse(s[1:]) + s[0]
Solution explanation
Each call moves the first character to the end of the reversed rest.
Show solution
def reverse(s):
if len(s) <= 1:
return s
return reverse(s[1:]) + s[0]
print(reverse(input())) Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module