When it is required to reverse a string using recursion technique, a user defined method is used along with recursion.
The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.
Below is a demonstration for the same −
def reverse_string(my_string): if len(my_string) == 0: return my_string else: return reverse_string(my_string[1:]) + my_string my_str = str(input("Enter the string that needs to be reversed : ")) print("The string is :") print(my_str) print("The reversed string is :") print(reverse_string(my_str))
Enter the string that needs to be reversed : Williw The string is : Williw The reversed string is : williW