Table of contents
1.
Introduction
2.
Using For Loop
3.
Using While Loop
4.
Using Recursion
5.
Using String Slicing
6.
Frequently Asked Questions
6.1.
Can these techniques handle negative numbers?
6.2.
Are these techniques efficient for very large numbers?
6.3.
Can these techniques reverse floating-point numbers?
7.
Conclusion
Last Updated: Aug 21, 2025
Easy

Python Program to Reverse a Number

Author Gaurav Gandhi
0 upvote

Introduction

In programming, a common task is to reverse a number in Python, such as turning 12345 into 54321. This can be done using loops, recursion, or string slicing. Reversing numbers has practical uses in cryptography, data processing, and is often asked in coding interviews.

Python Program to Reverse a Number

In this article, we will discuss different Python techniques to reverse a number, with code examples for each approach. 

Using For Loop

One straightforward way to reverse a number is by using a for loop. Let’s see how it works:

1. Take the number as input from the user & store it in a variable.
 

2. Convert the number to a string so we can iterate over each digit.
 

3. Create an empty string variable to store the reversed number.
 

4. Use a for loop to iterate over the digits of the number string in reverse order.
 

5. In each iteration, append the current digit to the reversed string.
 

6. After the loop ends, convert the reversed string back to an integer.
 

7. Print the reversed number.


For example: 

num = int(input("Enter a number: "))
num_str = str(num)
reversed_str = ""

for i in range(len(num_str)-1, -1, -1):
    reversed_str += num_str[i]

reversed_num = int(reversed_str)
print("Reversed number:", reversed_num)
You can also try this code with Online Python Compiler
Run Code


Output

Enter a number: 56
Reversed number: 65

 

In this code, we first convert the input number to a string using `str()`. Then, we create an empty string `reversed_str` to store the reversed number. The for loop iterates over the indexes of `num_str` in reverse order, starting from the last index `len(num_str)-1` & going backward until index 0. In each iteration, we append the digit at the current index to `reversed_str`. Finally, we convert `reversed_str` back to an integer using `int()` & print the reversed number.

Using While Loop

Another way to reverse a number is by using a while loop. This approach works as mentioned in the steps below:

1. Take the number as input from the user & store it in a variable.
 

2. Initialize a variable `reversed_num` to 0. This will store the reversed number.
 

3. Start a while loop that continues as long as the input number is greater than 0.
 

4. Inside the loop, use the modulo operator `%` to extract the last digit of the number.
 

5. Multiply `reversed_num` by 10 & add the extracted last digit to it.
 

6. Divide the input number by 10 (integer division) to remove the last digit.
 

7. Repeat steps 4-6 until the input number becomes 0.
 

8. After the loop ends, `reversed_num` will hold the reversed number.
 

9. Print the reversed number.


For example: 

num = int(input("Enter a number: "))
reversed_num = 0

while num > 0:
    last_digit = num % 10
    reversed_num = (reversed_num * 10) + last_digit
    num //= 10

print("Reversed number:", reversed_num)
You can also try this code with Online Python Compiler
Run Code

 

Output

Enter a number: 56
Reversed number: 65


In this code, we initialize `reversed_num` to 0. The while loop continues as long as `num` is greater than 0. Inside the loop, we extract the last digit of `num` using the modulo operator `%`. We then multiply `reversed_num` by 10 to shift the existing digits to the left & add the extracted last digit to it. Next, we update `num` by performing integer division by 10, effectively removing the last digit. This process repeats until `num` becomes 0. Finally, we print the reversed number stored in `reversed_num`.

Using Recursion

Recursion is a powerful technique where a function calls itself to solve a problem. We can use recursion to reverse a number in Python. Let’s see how it works:

1. Define a recursive function that takes a number as input.
 

2. Inside the function, check the base case: if the number is less than 10, return the number itself.
 

3. If the number is greater than or equal to 10, recursively call the function with the number divided by 10 (integer division).
 

4. Multiply the result of the recursive call by 10 & add the last digit of the current number to it.
 

5. Return the result.
 

6. Call the recursive function with the input number & print the reversed number.


For example

def reverse_number(num):
    if num < 10:
        return num
    else:
        return (num % 10) + (reverse_number(num // 10) * 10)

num = int(input("Enter a number: "))
reversed_num = reverse_number(num)
print("Reversed number:", reversed_num)
You can also try this code with Online Python Compiler
Run Code

 

Output

Enter a number: 87
Reversed number: 78


In this code, we define a recursive function called `reverse_number()` that takes the number `num` as input. The base case is when `num` is less than 10, in which case we simply return `num` itself. If `num` is greater than or equal to 10, we recursively call `reverse_number()` with `num // 10`, which removes the last digit. We then multiply the result of the recursive call by 10 & add the last digit of the current number (`num % 10`) to it. This process continues until the base case is reached.


To use the recursive function, we take the user's input number, call `reverse_number()` with the input number, and store the result in `reversed_num`. Finally, we print the reversed number.

Note: Recursion provides an elegant and concise way to reverse a number, although it may be less efficient for very large numbers than iterative approaches.

Using String Slicing

Python's string-slicing feature allows us to reverse a number in a concise & efficient manner. Let’s discuss how it works:
 

1. Take the number as input from the user & store it in a variable.
 

2. Convert the number to a string using the `str()` function.
 

3. Use string slicing with a step of -1 to reverse the string.
 

4. Convert the reversed string back to an integer using the `int()` function.
 

5. Print the reversed number.


For example: 

num = int(input("Enter a number: "))
num_str = str(num)
reversed_str = num_str[::-1]
reversed_num = int(reversed_str)
print("Reversed number:", reversed_num)
You can also try this code with Online Python Compiler
Run Code

 

Output

Enter a number: 56
Reversed number: 65


In this code, we first convert the input number `num` to a string using `str()` & store it in `num_str`. Then, we use string slicing with a step of -1 to reverse the string. The slicing syntax `num_str[::-1]` means:

  • Start from the end of the string (empty before the first colon)
     
  • Go until the beginning of the string (empty after the second colon)
     
  • Move with a step of -1 (negative step indicates backward direction)

 

This effectively reverses the string. We store the reversed string in `reversed_str`. Finally, we convert `reversed_str` back to an integer using `int()` & print the reversed number.

String slicing provides a simple, pythonic way to reverse a number. It is concise and efficient, especially for small—to medium-sized numbers.

Frequently Asked Questions

Can these techniques handle negative numbers?

Yes, the for loop, while loop, & recursion techniques can handle negative numbers. Simply consider the sign separately & reverse the absolute value of the number.

Are these techniques efficient for very large numbers?

The string slicing technique is efficient for large numbers. The for loop & while loop are also efficient, but recursion may cause a stack overflow for extremely large numbers.

Can these techniques reverse floating-point numbers?

No, these techniques are designed for reversing integers. To reverse a floating-point number, you would need to split the number into its integer & decimal parts, reverse each part separately, & then combine them back.

Conclusion

In this article, we discussed four different techniques for reversing a number in Python: using a for loop, a while loop, recursion, and string slicing. The for loop and while loop provide clear and straightforward implementations, recursion offers an elegant solution, and string slicing is concise and efficient. 

Recommended Readings:

 

Live masterclass