Python Recursive Function Check Palindrom String

By | March 20, 2020

Python Recursive Function Check Palindrom String

# Write a Python program to
# input a string and find
# whether the string is a palindrome or not

# www.easycodebook.com

# Start of the Python program

# define the recursive function
def is_palindrome(s):
    if len(s) <= 1: # Base case
        return True
    elif s[0] != s[len(s) - 1]: # Base case
        return False
    else:
        return is_palindrome(s[1 : len(s)-1])

# display message to enter string
# and get input string
str1 = input("Enter a string to check for palindrome:")

# call the reursive function
result = is_palindrome(str1)

# now print if string is palindrome or not

if result==True:
    print(str1, " is a palindrome")
else:
    print(str1, " is a not a palindrome")
Enter a string to check for palindrome:mom
mom  is a palindrome

Output 2:
Enter a string to check for palindrome:Python
Python  is a not a palindrome

Loading

One thought on “Python Recursive Function Check Palindrom String

  1. Pingback: Python User Defined and Built-in Functions - Defining and Using Functions | EasyCodeBook.com

Leave a Reply

Your email address will not be published. Required fields are marked *