Palindrome Number Check in Python

Check whether an integer is a palindrome using digit reversal.

BeginnerLoop ProgramsExample 8 of 25
palindrome-number-check.py
Run in browser
1# Program to check palindrome number
2
3num = int(input("Enter an integer: "))
4
5temp = abs(num)
6rev = 0
7
8while temp > 0:
9 digit = temp % 10
10 rev = rev * 10 + digit
11 temp //= 10
12
13if num >= 0 and rev == num:
14 print(num, "is a palindrome")
15else:
16 print(num, "is not a palindrome")

Output

Enter an integer: 121
121 is a palindrome

What's going on

We reverse the digits and compare with the original number; for simplicity, we treat negative numbers as non-palindromes.