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

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
Is it just me, or is string reversing difficult/surprising?
Stay up to date with the latest news, packages, and meta information relating to the Python programming language. --- If you have questions or are new to Python use r/LearnPython ... Yes, I realize that there are alternatives, but they are non-obvious (or at least not as obvious as str.reverse()). More on reddit.com
🌐 r/Python
97
44
July 12, 2015
🌐
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.
🌐
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))
🌐
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 ...
🌐
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.
Find elsewhere
🌐
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.
🌐
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
🌐
Plain English
python.plainenglish.io › solving-leetcode-14-reverse-an-integer-in-python-a89cf5244eab
Solving Leetcode 14: Reverse an Integer in Python | by Saul Feliz | Python in Plain English
November 27, 2020 - We’ll do this by setting our result variable to the output of making the input a string, reversing it, and then making it an integer again. We have to make the integer a string because a number is not iterable. Meaning you can’t do for loops or slicing methods on it.
🌐
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.
🌐
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.
🌐
Vultr
docs.vultr.com › python › examples › reverse-a-number
Python Program to Reverse a Number | Vultr Docs
December 9, 2024 - This code snippet converts a number to a string and uses string slicing ([::-1]) to reverse the order of characters. It then casts the reversed string back to an integer.
🌐
YouTube
youtube.com › watch
Python Program to Reverse a Number ( using String Method Tutorial ) - YouTube
In this tutorial you will learn to Reverse a Number ( using String method ) in Python Programming language.We ask the user to enter a number and store it in ...
Published   September 10, 2020
🌐
Medium
medium.com › edureka › reverse-a-number-6eeb7eed0309
How to reverse a number in Python? | Edureka
February 4, 2021 - 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 RecursionNum = 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)
🌐
YouTube
youtube.com › watch
Reverse Integer - Bit Manipulation - Leetcode 7 - Python - YouTube
🚀 https://neetcode.io/ - A better way to prepare for Coding Interviews🐦 Twitter: https://twitter.com/neetcode1🥷 Discord: https://discord.gg/ddjKRXPqtk🐮 S...
Published   August 18, 2021
🌐
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.
🌐
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.
🌐
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.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › write-a-program-to-reverse-digits-of-a-number
Write a program to reverse digits of a number - GeeksforGeeks
DSA Python · Last Updated : 21 May, 2025 · 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   May 21, 2025