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.

🌐
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.
🌐
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 …
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › reverse-a-string-using-recursion
Print reverse of a string using recursion - GeeksforGeeks
March 6, 2025 - Input: s = "Reverse a string Using Recursion" Output: "noisruceR gnisU gnirts a esreveR" Explanation: After reversing the input string we get "noisruceR gnisU gnirts a esreveR".
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › python-program-to-reverse-a-string-using-recursion
Python Program to Reverse a String Using Recursion
The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem. ... def reverse_string(my_string): if len(my_string) == 0: return my_string else: return reverse_string(my_string[1:]) + my_string[0] my_str = str(input("Enter ...
🌐
Real Python
realpython.com › reverse-string-python
Reverse Strings in Python: reversed(), Slicing, and More – Real Python
July 31, 2023 - Here, you first compute the index of the last character in the input string by using len(). The loop iterates from index down to and including 0. In every iteration, you use the augmented assignment operator (+=) to create an intermediate string that concatenates the content of result with the corresponding character from text. Again, the final result is a new string that results from reversing the input string. ... You can also use recursion to reverse strings.
🌐
Flexiple
flexiple.com › python › python-reverse-string
Reverse String In Python - Flexiple
To reverse a string in Python using recursion, employ a function that recursively chops off the first character and appends it to the end.
Find elsewhere
🌐
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
🌐
GitHub
github.com › arnab132 › Reverse-String-using-Recursion-in-Python
GitHub - arnab132/Reverse-String-using-Recursion-in-Python: Implementation of Reverse String using Recursion in Python
If not equal to 0, the reverse function is recursively called to slice the part of the string except the first character and concatenate the first character to the end of the sliced string.
Author   arnab132
🌐
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) ...
🌐
Team Treehouse
teamtreehouse.com › community › reverse-string-using-recursion
Reverse string using recursion (Example) | Treehouse Community
June 13, 2015 - def reverse(t): print("t = " + t) if t == "": print("t is empty") return t else: return reverse(t[1:]) + t[0] print(reverse('hello world')) ... t = hello world t = ello world t = llo world t = lo world t = o world t = world t = world t = orld ...
🌐
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.
🌐
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
January 26, 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); } };
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-reverse-string
Python Reverse String - 5 Ways and the Best One | DigitalOcean
August 3, 2022 - $ python3.7 -m timeit --number ...loop("ABç∂EF"*10)' 100000 loops, best of 5: 9.4 usec per loop $ python3.7 -m timeit --number 100000 --unit usec 'import string_reverse' 'string_reverse.reverse_recursion("ABç∂EF"*10)' 100000 loops, best of 5: 24.3 usec per lo...
🌐
Javatpoint
javatpoint.com › how-to-reverse-a-string-in-python
How to reverse a string in Python - Javatpoint
Write Python Program to Find Uncommon Characters of the Two Strings · Write Python Program to Recursively Remove All Adjacent Duplicates · Write the Python Program to Reverse the Vowels in the Given String · How to use Pass statement in Python · Recursion in Python ·
🌐
GeeksforGeeks
geeksforgeeks.org › reverse-string-python-5-different-ways
How to reverse a String in Python - GeeksforGeeks
Looping & List comprehension provides more control over the reversal process. stack approach is least suitable here, but it helps in understanding the data structures & algorithmic thinking and problem-solving. ... A string is a sequence of characters. Python treats anything inside quotes as a string.
Published   November 21, 2024
🌐
Sololearn
sololearn.com › en › Discuss › 2701028 › given-a-string-as-input-use-recursion-to-output-each-letter-of-the-strings-in-reverse-order-on-a-new-line
Given a string as input, use recursion to output each letter of the strings in reverse order, on a new line. | Sololearn: Learn to code for FREE!
def reverse(text): n = len(text) if n: print(text[-1]) if 1 < n: reverse(text[:-1]) reverse('PYTHON') ... def spell(txt): #your code goes here while len(txt) > 0: print(txt[-1]) return spell(txt[:-1]) txt = input() spell(txt) ... def spell(txt): ...