Skip to content

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 = while original_number > : 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')
Code language: Python (python)

Option 2 – String Reversal Number Palindrome

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

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.')
Code language: Python (python)

See also  How to Delete a Specific Resource in Terraform
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x