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?

People also ask

Is it possible to reverse a floating-point number in Python?
Yes, but you would treat it as a string. Reverse the digits, including the decimal point, and then convert it back to a floating-point number if needed.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › reverse a number in python
How to Reverse a Number in Python: 5 Smart Ways with Code
Can I use a list comprehension to reverse a number in Python?
Yes, you can use a list comprehension to reverse the digits of a number by converting it into a string, iterating in reverse, and then converting it back to an integer. However, this is more commonly done with a for loop or slicing.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › reverse a number in python
How to Reverse a Number in Python: 5 Smart Ways with Code
How can I reverse a number in Python using a for loop?
You can reverse a number in Python using a for loop by converting the number to a string, iterating over the digits in reverse order, and then converting the reversed sequence back to an integer.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › reverse a number in python
How to Reverse a Number in Python: 5 Smart Ways with Code
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-reverse-a-number
Python Program to Reverse a Number - GeeksforGeeks
November 18, 2025 - Recursion works by repeatedly removing the last digit from the number and building the reversed number during the returning phase. It is elegant but inefficient compared to slicing or loops. ... def rev_num(n): if n < 10: return n return int(str(n % 10) + str(rev_num(n // 10))) n = 987654 print(rev_num(n))
🌐
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 - This problem tests a candidate’s ... to the Reverse Integer problem involves converting the integer into a string, reversing that string, and then converting it back to an integer....
🌐
Interviewing.io
interviewing.io › questions › reverse-integer
How to Reverse an Integer [Problem + Solution]
When tackling an algorithms problem one good place to start is by thinking about what you would do if you were solving the problem manually, and for this problem that approach is fairly straightforward. For example, if I were tasked with reversing the integer 321, I would flip/reverse the integer and then double-check to make sure the answer is within the 32-bit integer range.
Published   December 23, 2020
Find elsewhere
🌐
Scaler
scaler.com › home › topics › reverse a number in python
Reverse a Number in Python - Scaler Topics
June 21, 2024 - The article explored the various methods and their respective implementations that can be employed to reverse a number in python. Some involve changing the number into a string and using some string methods, while others involve while loop, for loop, recursion, or stacks.
🌐
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 ...
🌐
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 - Int conversion ensures we can work with numeric values while updating the reversed number. String conversion allows us to treat the digits of a number as individual elements, making it easier to manipulate and reverse the number. ... This method of reversing numbers using a for loop is useful when you prefer working with strings or need to apply additional string manipulation. Recursion can be used to reverse a number in Python by repeatedly extracting the last digit and appending it to the result.
🌐
W3Schools
w3schools.com › python › ref_list_reverse.asp
Python List reverse() Method
Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training
🌐
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 - 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.
🌐
Educative
educative.io › answers › how-to-reverse-a-given-number-in-python
How to reverse a given number in Python
Here’s the implementation of this string slicing approach in Python to reverse the number: ... Note: Here, we have provided an explanation of the code segment that differs from the previous implementation. ... Line 10: Convert the number to a string using the str() function so we can reverse it using slicing. Line 11: Reverse the string using slicing [::-1]. Line 14: Convert the reversed string back to an integer using the int() function.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › write-a-program-to-reverse-digits-of-a-number
Reverse Digits of a Number - GeeksforGeeks
DSA Python · Last Updated : 3 Apr, 2026 · Given an Integer n, find the reverse of its digits. Examples: Input: n = 122 Output: 221 Explanation: By reversing the digits of number, number will change into 221.
Published   April 3, 2026
🌐
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 - After exploring the various ways to reverse a number in Python, it is helpful to compare them side by side. This allows you to choose the best method based on ease, performance, and readability. Each approach has its strengths and trade-offs. ... Beginners: Start with string conversion or for loops. They are easy to understand and quick to implement. Intermediate coders: Use while loops or function-based arithmetic methods for better control and performance.
🌐
PREP INSTA
prepinsta.com › home › python program › reverse a number in python
Reverse of a number in Python | PrepInsta​
July 29, 2023 - Using the formula reverse = ( reverse * 10 ) + remainder , we keep changing the reverse value. Break down the Nunber using divide operator. Print the reverse variable. Let’s implement the above mentioned Logic in Python Language.
🌐
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.
🌐
Python Forum
python-forum.io › thread-32496.html
Reverse Function in Python
Hi everyone i am beginner in python i am facing with this exercice can anyone explained how to write this in Python : Exercice : ----------------------------- Write a function reverse that receives an integer n as a parameter and returns that in...
🌐
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))
🌐
GitConnected
levelup.gitconnected.com › reverse-integer-python-5d4fb52ab3cd
Solution: Reverse Integer (Python) | Level Up Coding
September 12, 2021 - Solution: Reverse Integer (Python) 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] …
🌐
Sololearn
sololearn.com › en › Discuss › 3042103 › help-reversing-a-int-list-in-python-
Help reversing a int list in python
June 5, 2022 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.