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
🌐
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.
🌐
Programiz
programiz.com › python-programming › examples › reverse-a-number
Python Program to Reverse a Number
Created with over a decade of experience and thousands of feedback. ... 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 of the num divided by 10 is stored in the variable digit.
Discussions

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
Reversing a number
Hint: strings are reversible More on reddit.com
🌐 r/learnpython
9
2
September 17, 2020
People also ask

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)
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)
🌐
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?

🌐
Scaler
scaler.com › home › topics › reverse a number in python
Reverse a Number in Python - Scaler Topics
June 21, 2024 - So, we divide the original number ... the reversed number in our variable. ... To reverse a number in Python by this method, we use the property of strings, which is 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.
🌐
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.
Find elsewhere
🌐
Sanfoundry
sanfoundry.com › python-program-reverse-given-number
Reverse a Number in Python - Sanfoundry
June 21, 2023 - Write a Python Program to reverse a given number. ... Reversing a number means changing the order of its digits. For example, if we reverse the number 124, we get 421.
🌐
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 - Now that you understand the step-by-step logic behind reversing a number using hardcoded values, let’s move to the next version — where we’ll make the program more dynamic by taking user input instead of hardcoding the number. Let’s take a number with trailing zeros and see what happens when we reverse it. num = 1200 rev = 0 while num != 0: digit = num % 10 rev = rev * 10 + digit num //= 10 print("Reversed number:", rev) ... You might be wondering — where did the zeros go? That’s because Python (and most programming languages) don’t preserve leading zeros in integers.
🌐
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.
🌐
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.
🌐
GitHub
gist.github.com › hossainlab › b58bd9b22dfeb5eeedba4ee37c007946
Python Program To Reverse a Given Number - Gist - GitHub
Python Program To Reverse a Given Number · Raw · reverse_number.md · n = int(input("Enter a number: ")) reversed_number = 0 while(n>0): last_digit = n reversed_number = reversed_number*10+last_digit n = n//10 print("The reversed number is: ",reversed_number) Take input from user and store in a variable called n · Set the counter, which is reverse_number= 0 · Check the n is greater than 0 · Get the last digit of entered number by using modulus operator(%) Store the last digit in reversed_number variable ·
🌐
Python Guides
pythonguides.com › reverse-a-number-in-python
How To Reverse A Number In Python?
March 20, 2025 - Args: number: The number to reverse Returns: The reversed number """ # Handle negative numbers is_negative = number < 0 number_str = str(abs(number)) # Use the reversed() function reversed_str = ''.join(reversed(number_str)) return -int(reversed_str) if is_negative else int(reversed_str) # ...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › reverse number in python
Reverse Number in Python | Top12 Methods of Reverse Number in Python
April 17, 2023 - Note: Follow the above steps for executing the python programs for reversing which are going to be discussed below. ... def reverse_for_loop(s): s1 = '' for c in s: s1 = c + s1 return s1 my_number = '123456' if __name__ == "__main__": print('Reversing the given number using for loop =', reverse_for_loop(my_number))
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
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 multiply our reversed number by 10 and add our digit · Finally, we return the floored result of our number divided by 10 (this essentially removes the number on the right) This process is repeated until our original number is equal to zero · It’s important to note, this approach only works for integers and will not work for floats. In the next section, you’ll learn how to use Python string indexing to reverse a number.
🌐
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 - In this blog, you’ll explore multiple ways to reverse a number in Python—using loops, recursion, and yes, even the easy way with strings (just so you know the difference). You’ll also learn how to handle edge cases like leading zeroes, negative numbers, and large integers. Once you’re comfortable with these core concepts, don’t stop there. Strengthen your Python skills further with our hands-on ... Machine Learning Courses, where logic meets real-world application. The concept of reverse a number in Python using while loop is a great way to practice looping and number manipulation.
🌐
NxtWave
ccbp.in › blog › articles › reverse-a-number-in-python
Reverse a Number in Python: Methods & Best Practices
On the other hand, mathematical operations provide a more algorithmic approach to manipulating digits instead of converting the number to a string. In addition, Python has built-in options like the reversed() function or the use of list slicing, ...
🌐
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)
🌐
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 - num = 123456 reversed_int = 0 negative = num < 0 num = abs(num) while num != 0: reversed_int = reversed_int * 10 + num % 10 # Extract the last digit and append it to the reversed number num //= 10 # Remove the last digit if negative: reversed_int = -reversed_int # Restore sign if negative print('Reversed Integer:', reversed_int) # Output: # Reversed Integer: 654321Code language: Python (python) Run
🌐
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. ... Pros: Very concise, easy for beginners. Cons: Involves type conversion. You can use a for loop to iterate over the digits of a number (after converting it to a string) and build the reversed number manually.