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 −
def check_palindrome(my_str): if len(my_str) < 1: return True else: if my_str == 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")
Enter the string : MalaM MalaM The string is MalaM The string is a palindrome