Python Practice › If-Else Programs › Program 16
Check a palindrome number
Write a Python program to check whether a number is a palindrome.
Input
121, 123
Output
121 is a palindrome 123 is not a palindrome
Approach
- Compare the number’s digits with their reverse.
Solution
for n in [121, 123]: if str(n) == str(n)[::-1]: print(n, "is a palindrome") else: print(n, "is not a palindrome")Output
121 is a palindrome 123 is not a palindrome
Line by line
Line 2
str(n)[::-1] reverses the digits as text.
Learn the concept
New to this topic? Read the lesson, then come back.