Check a palindrome number

EasyIf-Else Programs · 16 of 64

Write a Python program to check whether a number is a palindrome.

Advertisement
Input
121, 123
Output
121 is a palindrome
123 is not a palindrome

Approach

  1. Compare the number’s digits with their reverse.

Solution

Python
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.

More if-else programs

← All 64 If-Else Programs

Leave a Comment