Python program to check whether a number is Palindrome or not

Avinash Nethala
Apr 4, 2017 · 2 min read

A simple python program which checks whether a number is palindrome or not.

Palindrome or not — programminginpython.com

Generally, a number is said to be a palindrome number if its reverse is same as the original number .

For Example: 121 is a palindrome as its reverse is also 121 where as, 231 is not a palindrome as its reverse is 132.

Task :

To check whether a number is palindrome or not

Approach :

  • Read an input number using input() or raw_input().
  • Check whether the value entered is integer or not.
  • We convert that integer number to string str(num).
  • Now we use advanced slice operation [start:end:step] leaving start and empty and giving step a value of -1, this slice operation reverses the string.
  • Now check whether reverse is equal to num,
  • If reverse is equal to num, the number is palindrome
  • When reverse is not equal to num, it is not a palindrome

Program :

num = input('Enter any number : ')
try:
val = int(num)
if num == str(num)[::-1]:
print('The given number is PALINDROME')
else:
print('The given number is NOT a palindrome')
except ValueError:
print("That's not a valid number, Try Again !")

Output :

Palindrome or not - programminginpython.com
Palindrome or not - programminginpython.com
Palindrome or not — programminginpython.com
Palindrome or not - programminginpython.com
Palindrome or not - programminginpython.com
Palindrome or not — programminginpython.com
Palindrome or not - programminginpython.com
Palindrome or not - programminginpython.com
Palindrome or not — programminginpython.com

programminginpython.com

All about python

Avinash Nethala

Written by

An enthusiast Web Developer and Owner of justlaravel.com, programminginpython.com, mycodingcorner.com and other sites.

programminginpython.com

All about python

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade