Home » Python program to check whether a number is Palindrome or not

Python program to check whether a number is Palindrome or not

Python program to check whether a number is Palindrome or not

Hello folks, welcome back to programminginpython.com.  Here in this post, I will share a simple Python program that checks whether a number is a palindrome or not. Let’s code a simple program to check palindrome in Python.
Generally, a number is said to be a palindrome number if its reverse is the same as the original number.

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

 

https://www.youtube.com/watch?v=3VQNdXA2IPI”]

You can watch the video on YouTube here

Program on GitHub

Palindrome number – Code Visualization

Task :

To check whether a number is a palindrome or not

Approach :

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

Program on Github

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 :

Python program to check whether a number is Palindrome
Python program to check whether a number is Palindrome

 

Python program to check whether a number is Palindrome
Python program to check whether a number is Palindrome

Python program to check whether a number is Palindrome
Python program to check whether a number is Palindrome

 

Program on GitHub

Please feel free to check other math-related programs here or some basic programs here.

Course Suggestion

Want to be strong at OOP in Python? If yes, I would suggest you take the course below.
Course:
Object Oriented Programming in Python

 

Online Python Compiler

Leave a Reply

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