How to reverse a number in Python [11 Different Ways]

Do you know how to reverse a number in Python? This tutorial will discuss how to reverse a number in Python. And also we are going to cover the following given topics.

  • Reverse a number in python
  • How we can Reverse a number in Python using for loop
  • Reverse a number in Python without using for loop
  • Reverse a number in Python using the function
  • Reverse a number in Python using a while loop
  • How to Reverse a number in Python using recursion
  • Reverse a number in Python using an inbuilt function
  • Reverse a number in Python without using an inbuilt function
  • Reverse a number in Python using slicing
  • How to reverse the order of numbers in Python
  • How to Reverse a number in a list in Python
  • Reverse a number and check if it is a palindrome in Python
  • Reverse character in Python

Reverse a number in python

  • When a number is reversed, its digits are arranged so that the first digit comes first, the last digit comes last, the second last digit comes last, and so on, with the last digit coming at the end.
  • The list method, slice method, recursion method, and other pre-defined methods in Python can be used in conjunction with conditional statements to create reverse logical functions. The simplest way to carry out this process on any type of user-provided input is with the reverse() method.

Simple logic reversing of numbers in a mathematical way:

First, we will take an integer number and divide it by 10 until the remainder will not come in the reverse order.

First Iteration:

num= 675
Reminder = num %10
Reminder = 675%10 = 5

# Second iteration
reminder = Num%10
reminder = 67 %10 = 7 

Example:

By using the slicing method we can easily get the reversed number of the original input number in Python.

Source Code:

new_number = 76354
print(str(new_number)[::-1])

You can obtain the reversed string by using the string-slicing method. Start:stop:step has the value -1. The start point moves to the beginning and stops at the finish when you pass -1 as a step.

Here is the implementation of the following given code

Reverse a number in python
Reverse a number in python

This is how we can reverse an input number in Python.

Read: Python Program for even or odd

Reverse a number in Python using for loop

  • By utilizing a for loop and the characters from the back of the original string, we may use this technique to reverse a number. In this manner, a new string is created that is inverted since the last character is added first, then the second last, and so on, with the first character being added last.
  • Using the input() or raw_input() methods, the input number can be read. Next, determine if the value entered is an integer or not. Now check if the provided number is bigger than zero.
  • Create a variable called “reverse” and set its initial value to “0.” Utilizing the mod (%) operator, find the remaining portion for the specified input number. Add the remaining value to the variable reverse after multiplying it by 10.

Example:

input_number = '6783'

result = ''
for m in range(len(input_number), 0, -1):
   result += input_number[m-1]
print('The reverse number is =', result)

Here in this example first, we declare a variable and assign the integer number to it and then used the for loop method along with that len() method.

Here is the Screenshot of the following given code.

Reverse a number in Python using for loop
Reverse a number in Python using for loop

In this example we have understood how to reverse a number in Python by using the for loop method.

Read: Check if a list is empty in Python

Reverse a number in Python without using for loop

  • This section will discuss how to reverse a number in Python without using for loop.
  • The value must first be entered by the user and stored in the variable n. The while loop is executed, and the modulus operator is utilized to determine the number’s final digit. The next digit is then kept at position one, the next to last at position ten, and so forth.
  • The final step is to truly divide the number by 10 to eliminate the last digit. When the value of the number is 0, this loop ends and the number is then printed on the back.

Example:

new_number = int(input("Enter the value: "))
result = 0
while(new_number>0):
 remainder = new_number % 10
 result = (result * 10) + remainder
 new_number = new_number//10
# Print the result
 print("Reverse number:",result)

In this example, we will set the condition if the input number is less than to 0 then it will divide by 10 and display the result.

You can refer to the below Screenshot of the following given code

Reverse a number in Python without using for loop
Reverse a number in Python without using for loop

This is how we can reverse a number in Python without using the for-loop method.

Read: Python square a number

Reverse a number in Python using the function

  • In this example, we will use the reversed() function to get the reverse of a number in Python.
  • The reversed() function returns an object to iterate through the components in reverse order but does not actually reverse anything. The reversed() method provides an iterator to aid in traversing the provided list in reverse order, however it modifies the list.
  • In order to reverse a number, we can easily use the Python reversed() function. A pointer that may iterate the string or list in reverse is the function’s return value. After that, we can extract the reversed number from this iterator using Python’s join() method.

Example:

new_number = 65783
con_str = str(new_number)
#By using the reversed method
result = "".join(reversed(con_str))
#Display reversed number
print("Reversed Number is:",result)

In the following given code first, we declared the input number and then converted the number into string. After that we have used the reversed() function and assign the converted string to it. After executing this code it will display the reverse number of a original number.

Here is the execution of the following given code

Reverse a number in Python using the function
Reverse a number in Python using the function

This is how we can reverse a number in Python using the reversed function.

Read: Python print without newline

Reverse a number in Python using a while loop

  • Here we will discuss how to use the while loop concept for getting the reverse number of an original number in Python.
  • We need to add the last digit first since we will start a new variable to keep our inverted number. We can take the modulo of the number by ten in the while loop to obtain the final digit. The final digit will reveal the solution to this.

Example:

new_number = 8656
new_output = 0

while new_number != 0:
    new_val = new_number % 10
    new_output = new_output * 10 + new_val
    new_number //= 10

print("Display the reversed Number: " + str(new_output))

Here is the Screenshot of the following given code

Reverse a number in Python using a while loop
Reverse a number in Python using a while loop

As you can see in the Screenshot we have learned how to use the while loop concept for getting the reverse number of an original number in Python.

Read: 11 Python list methods

Reverse a number in Python using recursion

  • Recursion is used to reverse a number in this way. Recursion has the drawback that it allows for infinite calls to the recursive function until the last index is reached.
  • The letters can then be added to the resulting reversed string in this basic instance, which will serve as our starting point. The last index will be inserted first, then the second last index, and so on, with the first index being added at the end. So, by using the recursive function first and then adding the letter, we may reverse the string.

Example:

new_num = 0
def reverse(new_val):
    global new_num
    if(new_val > 0):
        Reminder = new_val %10
        new_num = (new_num *10) + Reminder
        reverse(new_val //10)
    return new_num

new_val = int(input(" Enter the number : "))
new_num = reverse(new_val)
print(" The Result reverse number is:",new_num)

Here is the Output of the following given code

Reverse a number in Python using recursion
Reverse a number in Python using recursion

This is how to reverse a number in Python by using the recursion method.

Read: Python naming conventions

Reverse a number in Python using an inbuilt function

  • Here we discuss how to reverse a number in Python by using the inbuilt function.
  • The functions with pre-defined functionality in Python are referred to as built-in functions. There are various functions available for usage on the Python interpreter at all times.
  • In this example, we will use the reversed() inbuilt function which is available in Python and Python has a built-in method called reversed that returns an iterator of a sequence that has been reversed.

Example:

Let’s take an example and check how to reverse a number in Python by using the in-built function.

Source Code:

def new_number(m):  
   result = ''.join(reversed(m))
   return result

new_val = input('Enter the value: ')
# Display the result
print('Reverse number is:', new_number(new_val))

You can refer to the below Screenshot

Reverse a number in Python using an inbuilt function
Reverse a number in Python using an inbuilt function

In this example we have understood how to reverse a number in Python by using the inbuilt function.

Read: Python Addition Examples

Reverse a number in Python using slicing

  • In this approach, we’ll reverse the number using string-slicing techniques. The number will first be converted to a string, after which the string will be divided up and added in reverse order using the Python language’s string-slicing capability. We will perform the following operations for a given integer input.
  • Change the number’s format to a string and then make use of string slicing to reverse the number.
  • By using this technique, we can reverse a number by using the string-slicing method. In Python, slicing is the process of retrieving a string’s substring.

Example:

Let’s take an example and check how to reverse a number in Python by using the slicing method.

new_num = 73485
str_val = str(new_num)
m = len(str_val )
result = str_val [m::-1]

#output reversed number
print("Reversed Number is:", result)

Here is the implementation of the following given code

Reverse a number in Python using slicing
Reverse a number in Python using slicing

As you can see in the Screenshot we have learned how to reverse a number in Python by using the slicing method.

Read: Unexpected EOF while parsing Python

How to reverse the order of numbers in Python

  • In this section, we will discuss How to reverse the order of numbers in Python.
  • A number can be reversed by moving an input variable from the back to the front of the input number. When a number is reversed, its digits are arranged so that the first digit comes first, the last digit comes last, the second last digit comes last, and so on, with the last digit coming at the end.

Example:

input_number = '5678'

result = ''
for m in range(len(input_number), 0, -1):
   result += input_number[m-1]
print('The reverse number is =', result)

You can refer to the below Screenshot

How to reverse the order of numbers in Python
How to reverse the order of numbers in Python

In this example, we have understood How to reverse the order of numbers in Python.

Read: Python invalid literal for int() with base 10

Reverse a number in a list in Python

  • In this technique, we make use of the fact that lists in Python provide a reverse() method that flips the elements of the list.
  • So, using Python’s list() function, we simply add the digits of our number to the list, reverse them to get them in the opposite order, and then join them to get the final reversed number.
  • To combine a string with an iterable object, use Python’s join() function. The iterable’s strings are concatenated to create a brand-new string as a result. If the iterable includes any non-string values, it raises a TypeError exception.

Example:

new_num = "9432"
# Convert input num into list
new_lis = list(new_num)
# By using the reverse() method
new_lis .reverse()
#converting list into number
new_num = ''.join(new_lis )
print (" Result of input number is: ", new_num)

Here is the Output of the following given code.

Reverse a number in a list in Python
Reverse a number in a list in Python

This is how we can create a reverse number in a list in Python.

Read: Comment lines in Python

Reverse a number and check if it is a palindrome in Python

  • A palindrome is a word or a set of numbers made up of letters that spell the same word when reading both forward and backward. Python Palindrome permits punctuation, symbols, letters, and spaces within the Palindrome words.
  • When reversed, a number must remain the same to qualify as a palindrome. It is not a palindrome if the number does not equal the reverse of itself.

Example:

new_val=input(("Enter the value:"))
new_reverse = int(str(new_val)[::-1])

if new_val == new_reverse:
  print('It is Not Palindrome')
else:
  print("It is Palindrome")

Here is the Screenshot of the following given code

Reverse a number and check if it is a palindrome in Python
Reverse a number and check if it is a palindrome in Python

Read: Python check if the variable is an integer

Reverse character in Python

  • In this section, we will discuss how to reverse a character or string in Python.
  • In this example, we will create a slice that goes backward and begins at the end of the string.
  • In this instance, the slice phrase [::-1] denotes a move rearward step of one step, starting at position 0 and ending at the end of the string.

Example:

new_char = "USA" [::-1]
print(new_char)

Here is the implementation of the following given code

Reverse character in Python
Reverse character in Python

Also, take a look at some more Python tutorials.

In this article, we will discuss how to reverse a number in Python. And also we are going to cover the following given topics.

  • Reverse a number in python
  • Reverse a number in Python using for loop
  • Reverse a number in Python without using for loop
  • Reverse a number in Python using the function
  • Reverse a number in Python using a while loop
  • Reverse a number in Python using recursion
  • Reverse a number in Python using an inbuilt function
  • Reverse a number in Python without using an inbuilt function
  • Reverse a number in Python using slicing
  • How to reverse the order of numbers in Python
  • Reverse a number in a list in Python
  • Reverse a number and check if it is a palindrome in Python
  • Reverse character in Python