Python program to check the palindrome number or string



A palindrome number or a string is a word, number, phrase, or sequence that reads the same backward as forward. For example, Malayalam is a word that can be read the same backward as forward. Similarly, with regard to numbers such as 1001, 1221, etc is also a palindrome number.

The source code displayed below works for both numbers and strings. The program uses the simple logic of checking a string in reverse order.

Source code:

def ispalindrome(txt):
    if txt == txt[::-1]:
        print(f"{txt} is a palindrome!")
    else:
        print(f"{txt} is not a palindrome!")
t = input("Enter the word to check for palindrome:")
ispalindrome(t)

Output:

Enter the word to check for palindrome:malayalam
malayalam is a palindrome!
>>> 
Enter the word to check for palindrome:onion
onion is not a palindrome!
>>>
Enter the word to check for palindrome:1221
1221 is a palindrome!
>>> 
Enter the word to check for palindrome:2022
2022 is not a palindrome!