How to Reverse an Integer in Python


If you need to reverse an integer using Python, then you can do the following:

Option 1 – Mathematical Palindrome Check

original_number = 123454321
copy_number = original_number
reversed_number = 0

while original_number > 0:
    remainder = original_number % 10
    reversed_number = reversed_number * 10 + remainder
    original_number = original_number // 10

if copy_number == reversed_number:
    print(copy_number, 'is a palindrome number')
else:
    print(copy_number, 'is not a palindrome number')

Option 2 – String Reversal Number Palindrome

number = 123454321

if number == int(str(number)[::-1]):
    print(number, 'is palindrome.')
else:
    print(number, 'is not palindrome.')

Option 3 – List Reversal Number Palindrome

original_number = 123454321
lst_number = list(str(original_number))
lst_number.reverse()
reversed_number = ''.join(lst_number)

if (original_number == int(reversed_number)):
    print(original_number, 'is palindrome.')
else:
    print(original_number, 'is not palindrome.')