Thursday, 11 July 2019

12th Prog-4 Write a recursive Python program to test if a string is a palindrome or not.

def is_palindrome(s):
    if len(s) < 1:
        return True
    else:
        if s[0] == s[-1]:
            return is_palindrome(s[1:-1])
        else:
            return False
a=str(input("Enter string:"))
if(is_palindrome(a)==True):
    print("String is a palindrome!")
else:
    print("String isn't a palindrome!")



Output

Enter string:121
String is a palindrome!

Enter string:45
String isn't a palindrome!

No comments:

Post a Comment