How to Reverse a Number in Python

Sometimes, in a programming language, there is a need for reverse numbers in code to process data. Reversing means that the last digit will come at the beginning and the second last will come after that, and so on. In Python, the number is reversed to carry out any specific logical operation.

This write-up will provide a detailed understanding of the following terminologies:

So let’s get started!

Method 1: Using a While Loop

In Python, a while loop is also used to reverse any number or character with the help of a simple algorithm. The algorithm is defined inside the while loop, and it will run until the original number is not “equal to zero”. The given code below explained the working of while loop to get the reverse number in Python:

Code

orginal_num = 143947
number_reves = 0

while orginal_num != 0:
    value = orginal_num % 10
    number_reves = number_reves * 10 + value
    orginal_num //= 10

print("Reversed Number of Original Number is : " + str(number_reves))

In the above code:

  • The variables named “orginal_num” and “number_reves” are initialized.
  • In the main body of the “while loop”, the original number is divided by “10” and stored in the variable named“value”.The last digit of the input number or reminder is stored in this variable.
  • The reversed variable’s value is multiplied by “10” and added to the previous variable named “value”.
  • The original number is divided to eliminate the last digit.
  • The while loop iteration will continue until the condition “orginal_num !=0” is satisfied.
  • After the condition failed, “while loop” quit the function and printed the reversed number by converting it into a string value.

Output

The output shows the reversed number of the original input number.

Method 2: Using reversed() Method

The “reversed()” method reverses the number or string by iterating it in reverse order. The result obtained from reversed order is concatenated into a string with the help of the “join()” function.

Let’s understand this method by an example given below:

Code

orginal_num = 143947
string_num = str(orginal_num)
number_reves = "".join(reversed(string_num))
print("Reversed Number =  " + number_reves)
integer_number=int(number_reves)
print(type(integer_number))

In the above code:

  • The variable named “orginal_num” is initialized and converted into a string variable named “string_num”.
  • The “join()” function with the “reversed()” method reverses the number and concatenates the string.

Note: The “int()” function is used to convert the string value back into the integer for further calculation.

Output

The output shows the reverse number of the original number.

Method 3: Using Recursion Method

In Python, the recursive function can call itself during the program code. Using this method, the function is defined at once and calls the function automatically until the given number reaches its last index position.

Let’s understand the concept of the recursive function used to reverse a number in python by the following code.

Code

#defining the function of reverse
def reverse_num(n) :
    if len(n)==0 :
        return n
    
    return reverse_num(n[1:]) + n[0]

orginal_num = 143947
string_num = str(orginal_num)
number_reves = reverse_num(string_num)

print("Reversed Number =  " + number_reves)
integer_number=int(number_reves)
print(type(integer_number))

In the above code:

  • A recursive function named “reverse_num” is defined.
  • The recursive function adds the first digit to the end of the list and calls itself several times in a program until the original number value reaches the last index number.
  • The “original num” variable is initialized and converted into a string value. The new value is stored in a new variable named “string_num”.
  • The recursive function is called by passing the string value into his parameter.
  • The “int()” function converts the string value back into the integer for further calculation.

Output

The output shows the reversed number of the original number.

Method 4: Using for loop

Like the While loop, Python “for loop” is also used to reverse the number. The “for loop” reverses the number by adding the last digit into a new empty string and continuing until the index value reaches the first digit.

Let’s understand with the following example:

Code

orginal_num = 684524
string_num = str(orginal_num)

number_reves = ''
for i in range(0,len(string_num)):
    
    number_reves = string_num[i] + number_reves
    
print("Reversed Number of Original Number is : " + number_reves)

integer_number=int(number_reves)
print(type(integer_number))

In the above code:

  • The variable “orginal_num” is initialized.
  • Original number is converted into a string and saved into the new variable “string_value”.
  • The “for loop” is used to add the character value from the end of the string named “string_num” into a new empty string named “number_reves”.
  • The addition starts from the “last digit” number and continues until the “first digit” number of the original number is reached.

Note: The “int()” function is used to convert the string value back into the integer for further calculation. Any mathematical operation like addition and multiplication is easily performed using an integer value.

Output

The output shows the reversed number of the original input number.

Method 5: Using String Slicing

Slicing is used to get the substring from any string. This method is used to get the substring from the end/start side of the string with the help of an index number. Using the slicing technique, we have to convert the number to string and then reverse it.

Let’s understand the concept by an example given below:

Code

orginal_num = 96482
string_num = str(orginal_num)[::-1]
print('reversed number of original number is: ',string_num)
integer_number=int(string_num)
print(type(integer_number))
#perform any operation on integer value
val = integer_number +98
print(val)

In the above output:

  • The variable named “orginal_num” is initialized.
  • The original value is converted into a string.
  • The slicing parameter value passed “-1” to reverse the number as “-1” can give us the substring from the end of a given string.
  • The reversed number is printed with the help of a print statement.
  • The “int()” function converts the string value back into the integer for further calculation.
  • Integer value is added into “98” and printed on the screen.

Output

The above output shows the reverse number of the original number.

Conclusion

In Python, the “for loop”, “while loop”, “string slicing”, “recursive method”, and “reversed() function” are used to reverse a number. Some methods convert the original number into a string and then perform string methods to reverse the number. Other methods like “for loop” and “while loop” define their algorithm inside the main body to reverse the number. To perform any operation on a number, we convert the string value back into the integer using the “int() function”. This post briefly covered all the methods to reverse a number in Python with numerous examples.