Javaexercise.com

Python Program to Check Palindrome Number

A palindrome number is a number that is equal to the reverse of itself. The palindrome number remains the same if its digits are reversed. For example, the number 121 is a palindrome because the reverse of it the same number i.e. 121. To find the palindrome number in Python, there can be several ways. Let's see some examples.

Palindrome Number Program in Python

This is a simple approach that uses a loop to reverse the number and then check whether it is equal to the original number. See, we used the int() method to parse user input as an integer because by default user input type is a string.

number =int(input("Enter a number:"))
hold=number
rev=0
while(number>0):
    rem=number%10
    rev=rev*10+rem
    number=number//10
if(hold==rev):
    print("This is a Palindrome Number")
else:
    print("This is not a Palindrome Number")

Output:

Enter a number: 1331
This is a Palindrome Number

 

Palindrome Number Program in Python

In this program, we used the string slicing technique to get the reverse of a number. We hold the reverse result into a variable and then check using the if condition whether the number is palindrome or not. 

number = input("Enter a number:")
reverse = number[::-1]
if(number==reverse):
    print("This is a Palindrome Number")
else:
    print("This is not a Palindrome Number")

Output:

Enter a number: 1331
This is a Palindrome Number

 

Palindrome Number Program in Python

This is a single line solution program. We use string slicing to reverse the number and then compare the result with the original number by using the double equal operator which returns a boolean value either true or false. It returns true if the number and its reverse both are the same. 

number = input("Enter a number:")
result = (number == number[::-1])
print("Is it a Palindrome Number ?",result)

Output:

Enter a number: 1331
Is it a Palindrome Number ? True