- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python Program to Reverse a String Using Recursion
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
- A method named ‘reverse_string’ is defined, that takes a string as a parameter.
- It checks the length of the string, and if it is not 0, then, the function is called again on all elements except the first element of the string, and the first element of the string is concatenated to the result of this fuction call.
- Outside the function, the user is asked to enter a string as input.
- The string is displayed on the console.
- The recursion function is called by passing this string as a parameter.
- It is displayed on the console as the output.
- Related Articles
- Python Program to Reverse a String without using Recursion
- Java program to reverse a string using recursion
- Python Program to Reverse a Stack using Recursion
- C++ program to Reverse a Sentence Using Recursion
- Java Program to Reverse a Sentence Using Recursion
- Golang Program to Reverse a Sentence using Recursion
- Python Program to Display the Nodes of a Linked List in Reverse using Recursion
- Python Program to Display the Nodes of a Linked List in Reverse without using Recursion
- Swift program to find the reverse of a given number using recursion
- Java Program to Reverse a String Using Stacks
- Golang program to reverse a string using stacks
- How to reverse a string in Python program?
- Python Program to Check Whether a String is a Palindrome or not Using Recursion
- Write program to reverse a String without using reverse() method in Java?
- Python Program to Print All Permutations of a String in Lexicographic Order using Recursion