
- 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 Program to Check Whether a String is a Palindrome or not Using Recursion
When it is required to check if a string is a palindrome or not using recursion technique, simple indexing and a user defined function, along with recutsion is used.
Palindromes are those strings or values which when read from left to right and right to left have the same characters in their respective indices.
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 −
Example
def check_palindrome(my_str): if len(my_str) < 1: return True else: if my_str[0] == my_str[-1]: return check_palindrome(my_str[1:-1]) else: return False my_string = str(input("Enter the string :")) print("The string is ") print(my_string) if(check_palindrome(my_string)==True): print("The string is a palindrome") else: print("The string isn't a palindrome")
Output
Enter the string : MalaM MalaM The string is MalaM The string is a palindrome
Explanation
- A method named ‘check_palindrome’ takes a string as a parameter.
- If the size of the string is less than one, ‘True’ is returned as output.
- Otherwise, the last element in the string is checked to see if it matches with the first element.
- The method is called again on the elements from second index to last index, where last index value would be excluded by design.
- Otherwise, the function returns false.
- Outside the function, the user is asked to enter a string.
- This string is displayed on the console.
- The method is called by passing this string as a parameter.
- If its value computes to ‘True’, relevant message is displayed on the console.
- Otherwise, a different message is displayed on the console.
- Related Articles
- How to Check Whether a String is Palindrome or Not using Python?
- Python program to check if a string is palindrome or not
- Program to check a string is palindrome or not in Python
- Python program to check whether the string is Symmetrical or Palindrome
- C Program to check if an array is palindrome or not using Recursion
- C++ Program to Check Whether a Number is Palindrome or Not
- C# program to check if a string is palindrome or not
- Program to check whether inorder sequence of a tree is palindrome or not in Python
- Python program to check whether a given string is Heterogram or not
- Program to check a number is palindrome or not without help of a string in Python
- Write a Golang program to check whether a given number is a palindrome or not
- Program to check string is palindrome with lowercase characters or not in Python
- Program to check string is palindrome or not with equivalent pairs in Python
- Program to check two parts of a string are palindrome or not in Python
- Write an example to find whether a given string is palindrome using recursion

Advertisements