;

Python Program to Check Whether a String is a Palindrome or not Using Recursion


Tutorialsrack 23/04/2020 Python

In this Python program, we will learn how to check whether a string is a palindrome or not using recursion.

What is Palindrome?

A palindrome is a word, number, phrase, or other sequences of characters that reads the same backward as forward, such as civic or rotator or the number 14241. 

A string is a palindrome when we reverse the string and after reversing the string, the reversed string is equal to the original string entered by the user.

Here is the code of the program to check whether a string is a palindrome or not using recursion.

Python Program to Check Whether a String is a Palindrome or not Using Recursion
# Python Program to Check Whether a String is a Palindrome or not Using Recursion

# Defining a Recursive Function
def is_palindrome(s):
    s = s.casefold()
    if len(s) < 1:
        return True
    else:
        if s[0] == s[-1]:
            return is_palindrome(s[1:-1])
        else:
            return False
        
# Take the Input from the User        
myStr=str(input("Enter the String: "))
if(is_palindrome(myStr)==True):
    print("String is a palindrome!")
else:
    print("String is not a palindrome!")
Output

Enter the String: Level

The string is a palindrome.

 

Enter the String: Tutorialsrack

The string is not a palindrome.


Related Posts



Comments

Recent Posts
Tags