0
0
PythonProgramBeginner · 2 min read

Python Program to Reverse a String Using Recursion

You can reverse a string in Python using recursion by defining a function like def reverse_string(s): return s if len(s) == 0 else reverse_string(s[1:]) + s[0] which calls itself with the string minus the first character and adds the first character at the end.
📋

Examples

Inputhello
Outputolleh
Inputa
Outputa
Input
Output
🧠

How to Think About It

To reverse a string using recursion, think of the string as a first character plus the rest. You call the function on the rest of the string, then add the first character at the end. Keep doing this until the string is empty, which is the stopping point.
📐

Algorithm

1
Get the input string.
2
Check if the string is empty; if yes, return it as is.
3
Otherwise, call the function recursively on the string except the first character.
4
Add the first character of the current string to the end of the result from the recursive call.
5
Return the combined string.
💻

Code

python
def reverse_string(s):
    if len(s) == 0:
        return s
    else:
        return reverse_string(s[1:]) + s[0]

print(reverse_string('hello'))
Output
olleh
🔍

Dry Run

Let's trace the string 'abc' through the recursive reverse_string function.

1

Initial call

reverse_string('abc') calls reverse_string('bc') and will add 'a' later.

2

Second call

reverse_string('bc') calls reverse_string('c') and will add 'b' later.

3

Third call

reverse_string('c') calls reverse_string('') and will add 'c' later.

4

Base case

reverse_string('') returns '' because string is empty.

5

Returning from third call

reverse_string('c') returns '' + 'c' = 'c'.

6

Returning from second call

reverse_string('bc') returns 'c' + 'b' = 'cb'.

7

Returning from initial call

reverse_string('abc') returns 'cb' + 'a' = 'cba'.

CallStringReturned Value
1abccalls reverse_string('bc') + 'a'
2bccalls reverse_string('c') + 'b'
3ccalls reverse_string('') + 'c'
4returns ''
3creturns 'c'
2bcreturns 'cb'
1abcreturns 'cba'
💡

Why This Works

Step 1: Base case stops recursion

When the string is empty (len(s) == 0), the function returns the empty string to stop further calls.

Step 2: Recursive call reduces problem size

The function calls itself with the string minus the first character (s[1:]), making the problem smaller each time.

Step 3: Building reversed string on return

After the recursive call returns, the function adds the first character (s[0]) to the end, gradually building the reversed string.

🔄

Alternative Approaches

Using slicing
python
def reverse_string(s):
    return s[::-1]

print(reverse_string('hello'))
This is the simplest and fastest way in Python but does not use recursion.
Using a loop
python
def reverse_string(s):
    result = ''
    for char in s:
        result = char + result
    return result

print(reverse_string('hello'))
Uses a loop to build the reversed string by prepending characters, no recursion involved.

Complexity: O(n) time, O(n) space

Time Complexity

The function calls itself once per character, so it runs in linear time proportional to the string length.

Space Complexity

Each recursive call adds a layer to the call stack, so space used is proportional to the string length.

Which Approach is Fastest?

Using slicing (s[::-1]) is fastest and uses less memory, but recursion is good for learning and understanding.

ApproachTimeSpaceBest For
RecursionO(n)O(n)Learning recursion and problem solving
SlicingO(n)O(1)Fastest and simplest in Python
LoopO(n)O(1)When avoiding recursion and slicing
💡
Always define a clear base case in recursion to avoid infinite calls.
⚠️
Forgetting the base case causes the recursion to never stop and crash with a stack overflow.