In the last tutorial we learned how to reverse a string in Python using loop. In this tutorial we will see how to reverse a string using recursion.
Program to Reverse String using Recursion
# 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) print("Reversed String is: ", reverse(mystr))
Output:
The Given String is: BeginnersBook Reversed String is: kooBsrennigeB
Source code in PyCharm IDE:
Leave a Reply