def rreverse(s):
    if s == "":
        return s
    else:
        return rreverse(s[1:]) + s[0]

(Very few people do heavy recursive processing in Python, the language wasn't designed for it.)

Answer from Fred Foo on Stack Overflow
Top answer
1 of 8
39
def rreverse(s):
    if s == "":
        return s
    else:
        return rreverse(s[1:]) + s[0]

(Very few people do heavy recursive processing in Python, the language wasn't designed for it.)

2 of 8
37

To solve a problem recursively, find a trivial case that is easy to solve, and figure out how to get to that trivial case by breaking the problem down into simpler and simpler versions of itself.

What is the first thing you do in reversing a string? Literally the first thing? You get the last character of the string, right?

So the reverse of a string is the last character, followed by the reverse of everything but the last character, which is where the recursion comes in. The last character of a string can be written as x[-1] while everything but the last character is x[:-1].

Now, how do you "bottom out"? That is, what is the trivial case you can solve without recursion? One answer is the one-character string, which is the same forward and reversed. So if you get a one-character string, you are done.

But the empty string is even more trivial, and someone might actually pass that in to your function, so we should probably use that instead. A one-character string can, after all, also be broken down into the last character and everything but the last character; it's just that everything but the last character is the empty string. So if we handle the empty string by just returning it, we're set.

Put it all together and you get:

def backward(text):
    if text == "":
        return text
    else:
        return text[-1] + backward(text[:-1])

Or in one line:

backward = lambda t: t[-1] + backward(t[:-1]) if t else t

As others have pointed out, this is not the way you would usually do this in Python. An iterative solution is going to be faster, and using slicing to do it is going to be faster still.

Additionally, Python imposes a limit on stack size, and there's no tail call optimization, so a recursive solution would be limited to reversing strings of only about a thousand characters. You can increase Python's stack size, but there would still be a fixed limit, while other solutions can always handle a string of any length.

๐ŸŒ
Medium
medium.com โ€บ @fridahwatetu โ€บ reversing-a-string-in-python-using-recursion-21346d959f68
Reversing a String In Python Using Recursion | by Fridah | Medium
August 8, 2023 - Reversing a String In Python Using Recursion string = "Hello World" def reversing_string(string): if (string == ""): return "" return reversing_string(string[1:]) + string[0] str โ€ฆ
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ python-program-to-reverse-a-string-using-recursion
Python Program to Reverse a String Using Recursion
The recursive approach works by taking the first character and appending it to the reversed substring. The base case is when the string becomes empty. ... def reverse_string(my_string): if len(my_string) == 0: return my_string else: return ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-reverse-string-string-reversal-in-python-explained-with-code-examples
Python Reverse String โ€“ String Reversal in Python Explained with Examples
November 10, 2021 - But since Python strings are immutable, you cannot modify or reverse them in place. In Python, there are a few different ways you can do this. And this tutorial will teach you how you can use string slicing, built-in methods, and recursion to reverse strings.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Reverse The Given String Using Recursion | Python Programs | Interview Question And Answer - YouTube
In this Python programming video series we will learn how to reverse the given string using recursion.Program 01:def reverse_str(str1): if str1 == "": ...
Published ย  February 1, 2021
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ python-program-reverse-string-using-recursion
Reverse a String in Python - Sanfoundry
June 19, 2023 - In this approach, we use recursion to reverse a string by repeatedly swapping the first and last characters. ... Here is source code of the Python Program to reverse a string using recursion.
Find elsewhere
๐ŸŒ
Source Code Tester
sourcecodester.com โ€บ tutorial โ€บ python โ€บ 18200 โ€บ how-reverse-string-using-recursion-python
How to Reverse a String Using Recursion in Python | SourceCodester
It repeatedly removes the first character and appends it to the reversed result of the remaining substring, until the string is empty. The user is prompted to enter a string, and the reversed version is displayed.
๐ŸŒ
BeginnersBook -
beginnersbook.com โ€บ home โ€บ python examples โ€บ python program to reverse a string using recursion
Python program to reverse a String using Recursion
June 6, 2018 - # Program published on https://beginnersbook.com # Python program to reverse a given String # Using Recursion # user-defined recursive function def reverse(str): if len(str) == 0: return str else: return reverse(str[1:]) + str[0] mystr = "BeginnersBook" print("The Given String is: ", mystr) ...
๐ŸŒ
Untitled Publication
fridah.hashnode.dev โ€บ reversing-a-string-in-python-using-recursion
Reversing a String In Python Using Recursion
June 15, 2023 - If the input string is not empty, the function makes a recursive call to reversing_string() with the argument string[1:]. This slices the input string from the second character onward and passes it as an argument to the recursive call.
๐ŸŒ
Career Karma
careerkarma.com โ€บ blog โ€บ python โ€บ python reverse string: a step-by-step guide
Python Reverse String: A Step-By-Step Guide | Career Karma
December 1, 2023 - You can reverse a string in Python using slicing or the reversed() method. A recursive function that reads each character in a string and reverses the entire string is another common way to reverse a string.
๐ŸŒ
Linode
linode.com โ€บ docs โ€บ guides โ€บ how-to-reverse-a-string-in-python
How to Reverse a String in Python | Linode Docs
May 13, 2022 - Working back from the deepest recursion level, the results recombine to form the new reversing_string. For the example string, the process of this working back looks something like this: "f" + "l" + "o" + "w" + " " + "m" + "o" + "o" + "d" While not the fastest of the approaches covered in this guide, the recursive function has the advantage of following functional programming principles. ... The Python interpreter enforces a limit to the number of recursions (or recursion depth) a function can have.
๐ŸŒ
Flexiple
flexiple.com โ€บ python โ€บ python-reverse-string
Reverse String In Python - Flexiple
March 18, 2024 - To reverse a string in Python using recursion, employ a function that recursively chops off the first character and appends it to the end.
๐ŸŒ
Reddit
reddit.com โ€บ r/askcomputerscience โ€บ trying to reverse a string using recursion, why is my program not working?
Trying to reverse a string using recursion, why is my program not working? : r/AskComputerScience
February 28, 2020 - I'm also not sure if the O(1) recursive like solution works in python because, as far as I know, python does not support tail call optimization. ... class Solution { public: void reverseString(vector<char>& s) { reverse(s, 0, s.size() - 1); } void reverse(vector<char>& s,int left,int right){ // stop when all characters have been swapped if(left> right){ return; } // swap first and last unreversed characters char temp = s[left]; s[left] = s[right]; s[right] = temp; // call the function on the next unreversed characters reverse(s, left + 1, right - 1); } };
๐ŸŒ
Javatpoint
javatpoint.com โ€บ how-to-reverse-a-string-in-python
How to reverse a string in Python - Javatpoint
How to reverse a string in Python with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, basics, data types, operators, etc.
๐ŸŒ
PythonForBeginners.com
pythonforbeginners.com โ€บ home โ€บ how to reverse a string in python
How to reverse a string in Python - PythonForBeginners.com
July 22, 2021 - Original String is: PythonForBeginners Reversed String is: srennigeBroFnohtyP ยท To use recursion to reverse a string, we will use the following procedure. Suppose we define a function reverseString(input_string) to reverse the string. First we will check if the input_string is empty, If yes ...
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-program-to-reverse-a-string
How To Reverse A String In Python?
January 12, 2026 - Using a loop gives you full control over the reversal process and helps you understand string manipulation and logic in Python. Recursion can also be used to reverse a string in Python.
๐ŸŒ
ScholarHat
scholarhat.com โ€บ home
How to Reverse a String in Python
September 11, 2025 - Using Recursion: Calls the function recursively, removing the last character each time. Using a Stack: TheLast-In-First-Out(LIFO) property of a stack is used to reverse the string.