Without converting the number to a string:

def reverse_number(n):
    r = 0
    while n > 0:
        r *= 10
        r += n % 10
        n /= 10
    return r

print(reverse_number(123))
Answer from Alberto on Stack Overflow
๐ŸŒ
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
Can anyone explain how to reverse a integer number?
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). More on reddit.com
๐ŸŒ r/learnpython
26
38
February 1, 2022
Endianness conversion of hex values in Python 2.x vs 3.x and ELI5 why bytes are used?
The decode method converts the string of hex digits into an actual sequence of bytes. You can't slice the string directly because in the string each character represents only 4 bits, but you want to reverse each 8 bit group (byte). Try printing the reprs of the intermediate values to understand what's going on. In python 3 they removed the ability to decode strings using a method because that operation very rarely makes sense and caused more problem that it solved. You can still use codecs on strings, it's just a little more roundabout now. import codecs x = 'abcd' d = codecs.encode(codecs.decode(x, 'hex')[::-1], 'hex').decode() This kind of stuff is easier if you just realise that you're working with bytes in the first place, rather than trying to keep hold of a string of digits. More on reddit.com
๐ŸŒ r/learnpython
5
0
November 13, 2014
Is it just me, or is string reversing difficult/surprising?
Here's the relevant StackOverflow question: http://stackoverflow.com/questions/931092/reverse-a-string-in-python The top answer's comments are full of debate about whether or not extended slicing is pythonic; the question and one other positive answer both mention that they both prefer str.reverse, ... More on reddit.com
๐ŸŒ r/Python
97
44
July 12, 2015
๐ŸŒ
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 - Converts the integer number into a string using str() function . For Example, str(1234) = "1234" ... The slicing syntax [::-1] is a powerful way to reverse a string in Python.
๐ŸŒ
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))
๐ŸŒ
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 optimized solution avoids string ... operations: Initialize a variable to hold the reversed number. Use a loop to extract each digit from x, append it to the reversed number, and remove it from x. Throughout the process, ...
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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 ...
๐ŸŒ
Python Examples
pythonexamples.org โ€บ reverse-a-number-in-python
Reverse a Number - Python Program
In this example, we convert given number to string using str() and then reverse it using string slicing. The reversed string is converted back to int. If the given input is not a number, we shall print a message to the user. n = 123456 reversed = int(str(n)[::-1]) print(reversed)
๐ŸŒ
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.
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ python-program-reverse-given-number
Reverse a Number in Python - Sanfoundry
June 21, 2023 - Here is the source code of the Python Program to reverse a given number using Slice Operator. number = int(input("Enter a number: ")) reversed_number = int(str(number)[::-1]) print("Reversed number:", reversed_number) ...
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ challenges โ€บ 1 โ€บ python-challenges-1-exercise-18.php
Python: Reverse the digits of an integer - w3resource
Python Code: 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)) Sample Output: 432 -432 ยท
๐ŸŒ
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.
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ reverse a number in python
How to Reverse a Number in Python: 5 Smart Ways with Code
November 11, 2024 - Finally, the reversed string is converted back to an integer using int(). This ensures the output is a numeric value, not a string. ... The print() statement outputs the reversed number, which is 19854. ... This method leverages Python's ability to work seamlessly with strings and lists, making the process intuitive and efficient.
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ reverse-integer-in-python
Reverse Integer in Python
August 1, 2020 - Reversing an integer number is an easy task. We may encounter certain scenarios where it will be required to reverse a number. Input: 12345 Output: 54321 There are two ways, we can reverse a number &minus
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python: reverse a number (3 easy ways)
Python: Reverse a Number (3 Easy Ways) โ€ข datagy
December 20, 2022 - Learn how to use Python to reverse a number including how to use reverse integers and how to reverse floats in with a custom function.
๐ŸŒ
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 - The reversed() method returns an iterator, which generates the digits in reverse order without creating a new list. In contrast, string slicing creates a reversed copy of the entire string in memory.
๐ŸŒ
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.
๐ŸŒ
Replit
replit.com โ€บ home โ€บ discover โ€บ how to reverse a number in python
How to reverse a number in Python | Replit
February 6, 2026 - The core of this technique is the slice notation [::-1]. It's a clean, Pythonic way to reverse any sequence, including our temporary string. The process is simple: Convert the number to a string with str(). Reverse the string using the [::-1] ...