How to Check Palindrome number in Python


In this example, we will learn how to examine if the input number is a palindrome in Python.

2. Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

def reversal(n):
    i = 0
    while n > 0:
        j = n % 10
        i = i * 10 + j
        n = n // 10
    return i

n = int(input("Please input a number: "))
rev = reversal(n)
if n == rev:
    print("This is a palindrome!")
else:
    print("This is not a palindrome!")

Output:

Please input a number: 12345654321
This is a palindrome!

Please input a number: 123456
This is not a palindrome!
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement