Reverse a Number
Learn how to reverse the digits of a number in Python.
Reverse a Number
Problem
Given a number, reverse its digits.
Input: 12345
Output: 54321
Input: 9800
Output: 89
Input: -456
Output: -654Logic
- If the number is negative, remember the sign and work with the positive version
- Extract each digit one by one using
% 10to get the last digit and// 10to remove it - Build the reversed number by multiplying the result by 10 and adding the new digit
- Put the negative sign back if needed
Flow
Solution 1 — using a loop and math
This solution does not use strings at all. It works purely with numbers — extracting digits using % and building the reversed number step by step.
def reverse_number(n):
# step 1 — handle negative numbers
is_negative = n < 0
# step 2 — work with positive version
n = abs(n)
# step 3 — build reversed number digit by digit
reversed_num = 0
while n > 0:
digit = n % 10 # get the last digit
reversed_num = reversed_num * 10 + digit # add digit to result
n = n // 10 # remove the last digit from n
# step 4 — restore the sign
if is_negative:
reversed_num = -reversed_num
return reversed_num
print(reverse_number(12345)) # 54321
print(reverse_number(9800)) # 89
print(reverse_number(-456)) # -654
print(reverse_number(0)) # 0Code Execution — Solution 1
Trace through reverse_number(12345):
| Loop | n | digit = n % 10 | reversed_num = reversed_num * 10 + digit | n = n // 10 |
|---|---|---|---|---|
| Start | 12345 | — | 0 | — |
| 1st | 12345 | 5 | 0 × 10 + 5 = 5 | 1234 |
| 2nd | 1234 | 4 | 5 × 10 + 4 = 54 | 123 |
| 3rd | 123 | 3 | 54 × 10 + 3 = 543 | 12 |
| 4th | 12 | 2 | 543 × 10 + 2 = 5432 | 1 |
| 5th | 1 | 1 | 5432 × 10 + 1 = 54321 | 0 |
| End | 0 | loop stops |
Result: 54321
n % 10 always gives you the last digit of a number. n // 10 removes the last digit. These two operations together let you walk through every digit of a number without converting to a string.
Solution 2 — using string slicing
Convert the number to a string, reverse it using [::-1], then convert back. The shortest and most readable approach.
def reverse_number(n):
# step 1 — handle negative numbers
is_negative = n < 0
# step 2 — work with positive version
n = abs(n)
# step 3 — convert to string, reverse, convert back
# int() automatically drops leading zeros — int("0089") = 89
reversed_num = int(str(n)[::-1])
# step 4 — restore the sign
if is_negative:
reversed_num = -reversed_num
return reversed_num
print(reverse_number(12345)) # 54321
print(reverse_number(9800)) # 89
print(reverse_number(-456)) # -654
print(reverse_number(0)) # 0Code Execution — Solution 2
Trace through reverse_number(9800):
| Step | Code | Result |
|---|---|---|
| Start | n = 9800 | n = 9800 |
| Is negative? | 9800 < 0 | is_negative = False |
| abs() | abs(9800) | n = 9800 |
| To string | str(9800) | "9800" |
| Reverse | "9800"[::-1] | "0089" |
| To int | int("0089") | reversed_num = 89 |
| Sign? | False | no change |
| Return | 89 |
[::-1] is Python slice syntax with step -1 — it reads a string from end to start. "9800"[::-1] becomes "0089". Then int("0089") drops the leading zero and gives 89.
Solution 3 — using a loop over string characters
Loop through each character of the string version of the number and build the reversed string manually.
def reverse_number(n):
# step 1 — handle negative numbers
is_negative = n < 0
# step 2 — work with positive version
n = abs(n)
# step 3 — convert to string and loop through characters in reverse
n_str = str(n)
reversed_str = ""
for char in n_str:
# each character is added to the FRONT of reversed_str
reversed_str = char + reversed_str
# step 4 — convert back to int
reversed_num = int(reversed_str)
# step 5 — restore the sign
if is_negative:
reversed_num = -reversed_num
return reversed_num
print(reverse_number(12345)) # 54321
print(reverse_number(9800)) # 89
print(reverse_number(-456)) # -654
print(reverse_number(0)) # 0Code Execution — Solution 3
Trace through reverse_number(12345):
| Loop | char | reversed_str = char + reversed_str |
|---|---|---|
| Start | — | "" |
| 1st | "1" | "1" + "" = "1" |
| 2nd | "2" | "2" + "1" = "21" |
| 3rd | "3" | "3" + "21" = "321" |
| 4th | "4" | "4" + "321" = "4321" |
| 5th | "5" | "5" + "4321" = "54321" |
| Done | int("54321") = 54321 |
Each new character is added to the front — not the back. This is what builds the reversal.
Which solution to use?
| Solution | How | Best when |
|---|---|---|
| Solution 1 | Loop + math (% 10, // 10) | You want to understand how digits work |
| Solution 2 | String slicing [::-1] | You want clean, minimal code |
| Solution 3 | Loop over string characters | You want to understand string building |
Output
54321
89
-654
0