DocsHub
PythonBeginner

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: -654

Logic

  1. If the number is negative, remember the sign and work with the positive version
  2. Extract each digit one by one using % 10 to get the last digit and // 10 to remove it
  3. Build the reversed number by multiplying the result by 10 and adding the new digit
  4. Put the negative sign back if needed

Flow

Yes No Yes No Yes No Start Is number negative? Remove minus sign Keep as is reversed = 0 n is greater than 0? digit = n % 10get last digit reversed = reversed x 10 + digit n = n // 10remove last digit Was it negative? Add minus sign Keep as is Return result

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))       # 0

Code Execution — Solution 1

Trace through reverse_number(12345):

Loopndigit = n % 10reversed_num = reversed_num * 10 + digitn = n // 10
Start123450
1st1234550 × 10 + 5 = 51234
2nd123445 × 10 + 4 = 54123
3rd123354 × 10 + 3 = 54312
4th122543 × 10 + 2 = 54321
5th115432 × 10 + 1 = 543210
End0loop 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))       # 0

Code Execution — Solution 2

Trace through reverse_number(9800):

StepCodeResult
Startn = 9800n = 9800
Is negative?9800 < 0is_negative = False
abs()abs(9800)n = 9800
To stringstr(9800)"9800"
Reverse"9800"[::-1]"0089"
To intint("0089")reversed_num = 89
Sign?Falseno change
Return89

[::-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))       # 0

Code Execution — Solution 3

Trace through reverse_number(12345):

Loopcharreversed_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"
Doneint("54321") = 54321

Each new character is added to the front — not the back. This is what builds the reversal.


Which solution to use?

SolutionHowBest when
Solution 1Loop + math (% 10, // 10)You want to understand how digits work
Solution 2String slicing [::-1]You want clean, minimal code
Solution 3Loop over string charactersYou want to understand string building

Output

54321
89
-654
0

On this page