Program to check palindrome in python

Last updated 4 years, 1 month ago | 1848 views 75     5

Tags:- Python

Python | Check whether the given value is palindrome or not

This is an example of how to check whether the given value is palindrome or not. It takes input from the user to check palindrome.

A string is said to be palindrome if the string and reverse of the string will be the same.

value1 = input('Enter value to check palindrome: ')

# reverse of value1
value2 = value1[::-1]

# check value and its reverses(value2) is same
if value1==value2:
    print(f'{value1} is palindrome')
else:
    print(f'{value1} is not palindrome')
 

Output for the above code:

Check for number

>>> 
====================== RESTART: E:\py\palindrome.py ======================
Enter value to check palindrome: 12321
12321 is palindrome
>>> 
====================== RESTART: E:\py\palindrome.py ======================
Enter value to check palindrome: 12345
12345 is not palindrome

Check for string

>>> 
====================== RESTART: E:\py\palindrome.py ======================
Enter value to check palindrome: madam
madam is palindrome
>>> 
====================== RESTART: E:\py\palindrome.py ======================
Enter value to check palindrome: sir
sir is not palindrome

Check for alphanumeric value

>>>
====================== RESTART: E:\py\palindrome.py ======================
Enter value to check palindrome: a121a
a2332a is palindrome
>>> 
====================== RESTART: E:\py\palindrome.py ======================
Enter value to check palindrome: a123a
a123a is not palindrome