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
🌐
W3Schools
w3schools.com › python › python_howto_reverse_string.asp
How to reverse a String in Python
There is no built-in function to reverse a String in Python. The fastest (and easiest?) way is to use a slice that steps backwards, -1. ... Create a slice that starts at the end of the string, and moves backwards.
🌐
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)) ... First, the remainder ...
People also ask

What are the benefits of using recursion to reverse a number in Python?
Recursion simplifies the process by repeatedly calling the function to break down the number and reverse it until it’s done.
🌐
wscubetech.com
wscubetech.com › resources › python › programs › reverse-number
Reverse a Number in Python (5 Different Ways)
How can I reverse a number in Python using a string?
You can convert the number to a string with str(), reverse it using slicing ([::-1]), and then turn it back into an integer using int().
🌐
wscubetech.com
wscubetech.com › resources › python › programs › reverse-number
Reverse a Number in Python (5 Different Ways)
Can I reverse a number using loops in Python?
Yes, you can reverse a number by using a while loop. Divide the number by 10 to extract digits and multiply the result by 10 to rebuild the reversed number step by step.
🌐
wscubetech.com
wscubetech.com › resources › python › programs › reverse-number
Reverse a Number in Python (5 Different Ways)
🌐
W3Schools
w3schools.com › python › ref_list_reverse.asp
Python List reverse() Method
Python Examples Python Compiler ... Q&A Python Bootcamp Python Certificate Python Training ... The reverse() method reverses the sorting order of the elements....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-reverse-a-number
Python Program to Reverse a Number - GeeksforGeeks
November 18, 2025 - This method reverses the number by converting it to a string, slicing it in reverse order and converting it back to an integer. ... This method extracts digits from the end of the number using % 10 and builds the reversed number digit-by-digit.
🌐
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
🌐
Python Examples
pythonexamples.org › reverse-a-number-in-python
Reverse a Number in Python
Popped digits are appended to form a new number which would be our reversed number. 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.
🌐
WsCube Tech
wscubetech.com › resources › python › programs › reverse-number
Reverse a Number in Python (5 Different Ways)
October 29, 2025 - Explore 5 different ways to reverse a number in Python. Get step-by-step code examples, outputs, and clear explanations to enhance your understanding.
🌐
Medium
medium.com › @ArunaKale › python-program-to-reverse-a-number-traditional-and-list-methods-explained-step-by-step-478de7b8e0db
How to Reverse a Number in Python: 4 Easy Methods (with Code Examples for Beginners) | by Aruna | Medium
December 17, 2025 - # Python Program to Reverse a Number (Hardcoded Value) # Step 1: Hardcode a number num = 12345 # Step 2: Initialize a variable to store the reversed number rev = 0 # Step 3: Use a while loop to reverse the number while num != 0: digit = num ...
🌐
Tutorial Gateway
tutorialgateway.org › python-program-to-reverse-a-number
Python Program to Reverse a Number Using While Loop
April 7, 2025 - Number = int(input("Please Enter any Number: ")) Reverse = 0 while(Number > 0): Reminder = Number  Reverse = (Reverse *10) + Reminder Number = Number //10 print("\n Reverse of entered number is = %d" %Reverse) This program allows the user to enter any positive integer. Then, that number is assigned to a variable Number. Next, Condition in the Python While loop ensures that the given number is greater than 0.
🌐
NxtWave
ccbp.in › blog › articles › reverse-a-number-in-python
Reverse a Number in Python: Methods & Best Practices
Master different techniques for reversing numbers in Python. Compare methods, handle edge cases, and optimize your code with step-by-step examples and solutions.
🌐
w3resource
w3resource.com › python-exercises › basic › python-basic-1-exercise-30.php
Python: Reverse the digits of a given number and add it to the original - w3resource
... # Function to reverse a number and add it to the original number until a palindrome is obtained def rev_number(n): s = 0 # Initialize a variable to count the number of iterations while True: k = str(n) # Convert the number to a string for ...
🌐
W3Schools
w3schools.com › python › ref_func_reversed.asp
Python reversed() Function
Python Examples Python Compiler ... Q&A Python Bootcamp Python Certificate Python Training ... The reversed() function returns a reversed iterator object....
🌐
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.
🌐
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?

🌐
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 - Learn how to reverse a number in Python using for loop, while loop, recursion, string slicing, reversed method, etc. Boost your Python basics with real examples.
🌐
PREP INSTA
prepinsta.com › home › python program › reverse a number in python
Reverse of a number in Python | PrepInsta​
July 29, 2023 - Run a while loop with the condition number >0. Extract the last digit as Remainder using Modulo operator. Using the formula reverse = ( reverse * 10 ) + remainder , we keep changing the reverse value. Break down the Nunber using divide operator.
🌐
Replit
replit.com › home › discover › how to reverse a number in python
How to reverse a number in Python
February 6, 2026 - The core of this technique is the slice notation [::-1]. This is a common Python idiom that creates a reversed copy of any sequence, including strings. Once the character order is flipped, int() converts the new string back into a number, completing ...
🌐
PYnative
pynative.com › home › python › programs and examples › python programs to reverse an integer number
Python Reverse a Number [4 Ways] – PYnative
March 31, 2025 - Learn to reverse a number in Python using different ways such string slicing, a mathematical approach, recursion and using list reversal
🌐
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)