Palindrome Number Check in Python

Check whether an integer is a palindrome using digit reversal.

BeginnerLoop ProgramsExample 8 of 25
palindrome-number-check.py
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.

After this program

If the idea is still fuzzy, read Day 9: Loops and Iteration in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 loop programs, or see every topic on Python exercises.