Start learning free

LESSON 214 OF 673 · WHILE LOOPS - CHALLENGES

Reverse a Number in Python: Build It Digit by Digit

Practice Read the full explanation

Given a non-negative integer n, return the number you get when you write its digits in reverse order.

Zeros at the end of n move to the front of the reversed number, where they don't count: 120 reversed is 21. If n is 0, the answer is 0.

 

Example 1:

Input: n = 123
Output: 321
Explanation: The digits 1, 2, 3 read backwards are 3, 2, 1.

Example 2:

Input: n = 120
Output: 21
Explanation: Backwards the digits are 0, 2, 1, and 021 is the number 21.

Example 3:

Input: n = 7
Output: 7
Explanation: A number with one digit stays the same.

 

Constraints:

  • 0 <= n <= 109
STEP 1 OF 5
    Ctrl + Enter to check

    PYTHON · WHILE LOOPS - CHALLENGES

    Reverse Number in Python, explained

    Problem overview

    The function reverseNumber receives a non-negative integer n and returns the number you get by writing its digits in reverse order. For 123 the answer is 321. For 120 the digits read backwards are 0, 2, 1, which is the number 21, because a leading zero doesn't count. A one-digit number stays the same, and 0 gives 0.

    Approach

    Take the digits off n from the right with n % 10 and n // 10, and put each one on the end of a new number, result. The last digit of n comes off first, and it should be the first digit of the answer, so the order is already right.

    To put a digit on the end of result, shift result one place left by multiplying by 10, which leaves a 0 at the end, then add the digit. Starting from 32 and moving the digit 1: 32 * 10 + 1 is 321.

    Step by step

    1. Start result at 0. Nothing has moved yet. For n = 0 the loop never runs and 0 is right.
    2. Loop while n > 0. The loop ends when every digit has been taken off.
    3. Set result to result * 10 + n % 10. This shifts the digits built so far and appends the last digit of n.
    4. Set n to n // 10. The digit is now in result, so it can be dropped.
    5. Return result after the loop.

    The loop on n = 123:

    n at the startresult beforeresult aftern after dropping
    1230312
    123321
    1323210

    And on n = 120:

    n at the startresult beforeresult aftern after dropping
    1200012
    12021
    12210

    The solution in Python

    class Solution:
        def reverseNumber(self, n: int) -> int:
            result = 0
            while n > 0:
                result = result * 10 + n % 10
                n = n // 10
            return result
    

    Details worth knowing:

    • Operator precedence. In result * 10 + n % 10, both * and % bind tighter than +, so it means (result * 10) + (n % 10).
    • Why trailing zeros vanish. For 120 the first digit moved is 0, and 0 * 10 + 0 is still 0. Nothing marks the zero as a digit until a nonzero digit comes after it. That matches the lesson's rule that leading zeros of the answer don't count.
    • The string version. int(str(120)[::-1]) also gives 21: the slice [::-1] reverses the text "120" into "021", and int reads that as 21. The loop does the same with arithmetic only.
    • No overflow. Python integers grow as needed, so even a reversed number that is larger than the input is safe.

    Why multiply by 10

    Each place in a number is worth ten times the place to its right. The number 321 is 3 * 100 + 2 * 10 + 1. When a new digit arrives on the right, every digit already in result moves one place to the left, which is a multiplication by 10, and the new digit takes the units place. Applying the same step to each digit that comes off n builds the reversed number from its first digit to its last.

    The reversal loses information when the input ends in zeros: reversing 120 gives 21, and reversing 21 gives 12, not 120. So reversing twice returns the original number only when it has no trailing zeros. For 472, reversing twice does give 472.

    Edge cases and common mistakes

    • Zero. reverseNumber(0) returns 0.
    • Numbers ending in zeros. 1000 and 1000000000 both give 1.
    • Multiplying after adding. (result + n % 10) * 10 adds a stray zero at the end of the answer.
    • Forgetting n = n // 10. The loop would never end.

    Testing

    • 123 gives 321.
    • 120 gives 21.
    • 7 gives 7.
    • 0 gives 0.
    • 1000 gives 1.
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free