That's hardly cheating, and is now I'd do it unless that method was explicitly forbidden (I had that once). Your div/mod way is more complicated, and in my experience, likely doesn't have much in the way of noticeable performance advantages anyway. Abusing strings is often a good solution surprisingly (but consider each case individually). Answer from carcigenicate on reddit.com
🌐
Reddit
reddit.com › r/learnpython › can anyone explain how to reverse a integer number?
r/learnpython on Reddit: Can anyone explain how to reverse a integer number?
February 1, 2022 -

Sorry if that sound like stupid, I'm just a newbie to programming

At first I thought it was easy and started writing code and I find myself with bunch of errors.

Then I came up with this:

number = 12345
reverse = str(number)[::-1]
print(reverse)

It's just cheating you can say, I converted integer into string and made that string reverse.

After a while I thought, I'm just stupid to code like this but then later I googled and I found this solution which is more complicated to understand:

number = 12345

reverse = 0
while number > 0:
  last_digit = number % 10
  reverse = reverse * 10 + last_digit
  number = number // 10

print(reverse)

Can anyone please explain what's going on here?

Discussions

string - Using Python, reverse an integer, and tell if palindrome - Stack Overflow
Using Python, reverse an integer and determine if it is a palindrome. Here is my definition of reverse and palindrome. More on stackoverflow.com
🌐 stackoverflow.com
Is there a more efficient way to reverse an int
Turn it into a string, reverse string, turn back into int. More on reddit.com
🌐 r/learnpython
26
4
February 24, 2019
Reversing a number
Hint: strings are reversible More on reddit.com
🌐 r/learnpython
9
2
September 17, 2020
Leetcode Reverse Integer Question

Please read this guide on how to format your code in your post. I'd be more than happy to help once it's formatted in a way that makes it easier to read.

https://www.reddit.com/r/javahelp/wiki/code_guides

More on reddit.com
🌐 r/learnjava
3
4
February 23, 2020
🌐
Programiz
programiz.com › python-programming › examples › reverse-a-number
Python Program to Reverse a Number
To understand this example, you should have the knowledge of the following Python programming topics: ... num = 1234 reversed_num = 0 while num != 0: digit = num % 10 reversed_num = reversed_num * 10 + digit num //= 10 print("Reversed Number: " + str(reversed_num))
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-reverse-a-number
Python Program to Reverse a Number - GeeksforGeeks
November 18, 2025 - Let's explore different methods to reverse a number in Python. This method reverses the number by converting it to a string, slicing it in reverse order and converting it back to an integer.
🌐
PYnative
pynative.com › home › python › programs and examples › python programs to reverse an integer number
Python Programs to Reverse an Integer Number
March 31, 2025 - A list reversal approach involves converting the integer into a list of its digits, reversing the order of the list, and then converting it back to an integer. This method utilizes Python’s built-in list manipulation capabilities, such as list() and reverse(), to efficiently reverse the digits.
🌐
Medium
medium.com › @reza.shokrzad › reverse-integer-problem-exploring-simple-and-optimized-python-solutions-8e6d6818c2bd
Reverse Integer Problem: Exploring Simple and Optimized Python Solutions | by Reza Shokrzad | Medium
June 14, 2024 - The Reverse Integer problem challenges us to reverse the digits of a given signed 32-bit integer, x. The key is to return x with its digits reversed, but there’s a catch: if reversing x causes the value to exceed the 32-bit signed integer range, the function should return 0.
🌐
LeetCode
leetcode.com › problems › reverse-integer
Reverse Integer - LeetCode
Reverse Integer - Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. Assume the environment does not allow you to store ...
Find elsewhere
🌐
w3resource
w3resource.com › python-exercises › challenges › 1 › python-challenges-1-exercise-18.php
Python: Reverse the digits of an integer - w3resource
def reverse_integer(x): sign = -1 if x < 0 else 1 x *= sign # Remove leading zero in the reversed integer while x: if x % 10 == 0: x /= 10 else: break # string manipulation x = str(x) lst = list(x) # list('234') returns ['2', '3', '4'] lst.reverse() x = "".join(lst) x = int(x) return sign*x print(reverse_integer(234)) print(reverse_integer(-234))
🌐
Javatpoint
javatpoint.com › how-to-reverse-a-number-in-python
How to reverse a number in Python - Javatpoint
How to reverse a number in Python with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, basics, data types, operators, etc.
🌐
Interviewing.io
interviewing.io › questions › reverse-integer
How to Reverse an Integer [Problem + Solution]
To retrieve all digits of the number except the last one, you can perform integer division by 10. You can continue to perform the mod operation on the remaining digits (until it returns zero) to capture all digits of the integer.
Published   December 23, 2020
🌐
Scaler
scaler.com › home › topics › reverse a number in python
Reverse a Number in Python - Scaler Topics
June 21, 2024 - In this way, the new string so formed is reversed, as the last character is added first to it, followed by the second last, and so on and the first character is added last. ... We use the python reversed() function to reverse a number by this method.
🌐
datagy
datagy.io › home › python posts › python: reverse a number (3 easy ways)
Python: Reverse a Number (3 Easy Ways) • datagy
December 20, 2022 - We can use a Python while loop with the help of both floor division and the modulus % operator. Let’s take a look at an example to see how this works and then dive into why this works: # How to Reverse a Number with a While Loop number = 67890 reversed_number = 0 while number != 0: digit = number % 10 reversed_number = reversed_number * 10 + digit number //= 10 print(reversed_number) # Returns: 9876
🌐
Code and Debug
codeanddebug.in › home › data structures & algorithms › leetcode #7 : reverse integer python program explained
Leetcode #7 : Reverse Integer Python Program Explained
July 7, 2025 - Objective: The code aims to solve the problem of reversing the digits of a 32-bit signed integer “x”. Purpose: The function reverse is designed to take an integer “x”, reverse its digits, and return the reversed integer.
🌐
Sanfoundry
sanfoundry.com › python-program-reverse-given-number
Reverse a Number in Python - Sanfoundry
June 21, 2023 - Here is a program that reverse a number in Python using the while loop, slice operator and recursive approaches, along with detailed explanation & examples.
🌐
Edureka
edureka.co › blog › how-to-reverse-a-number
How to reverse a number in Python | Python Program Explained | Edureka
September 24, 2019 - Sixth iteration From the Second Iteration, the values of both Number and Reverse have been changed as, Number = 1 and Reverse = 65432 Reminder = Number  Reminder = 1  = 1 Reverse = Reverse *10+ Reminder = 65432 * 10 + 1 Reverse = 654320 + 1 = 654321 Number ended: # Python Program to Reverse a Number using Recursion Num = int(input("Please Enter any Number: ")) Result = 0 def Result_Int(Num): global Result if(Num > 0): Reminder = Num  Result = (Result *10) + Reminder Result_Int(Num //10) return Result Result = Result_Int(Num) print("n Reverse of entered number is = %d" %Result)
🌐
NxtWave
ccbp.in › blog › articles › reverse-a-number-in-python
Reverse a Number in Python: Methods & Best Practices
This is an illustration of how to use functional programming to reverse a number in Python. Convert the number to a string and use the reduce function to accumulate the digits in reverse order. Convert the final reversed string back to an integer.
🌐
Python Examples
pythonexamples.org › reverse-a-number-in-python
Reverse a Number - Python Program
n = 123456 reversed = 0 while(n!=0): r=int(n) reversed = reversed*10 + r n=int(n/10) print(reversed) ... In this tutorial of Python Examples, we learned how to reverse a number using while loop and string slicing.
🌐
Upgrad
upgrad.com › home › blog › data science › learn the techniques: how to reverse a number in python efficiently
Learn How to Reverse a Number in Python Efficiently
October 10, 2025 - Python makes it simple by converting numbers into strings. You can reverse the string and convert it back to an integer.
🌐
AlgoMonster
algo.monster › liteproblems › 7
7. Reverse Integer - In-Depth Explanation
In-depth solution and explanation for LeetCode 7. Reverse Integer in Python, Java, C++ and more. Intuitions, example walk through, and complexity analysis. Better than official and forum solutions.