Python Program for Reverse of a Number Using Type Casting
Last Updated :
05 Sep, 2024
Reversing a number is a common problem-solving exercise in programming, often used to teach the basics of algorithms and data manipulation. In Python, reversing a number can be efficiently achieved using type casting.
Example:
Input: 12345
Output: 54321
Input: -12345
Output: -54321
Input: 123.45
Output: 54.321
In this article, we are going to learn how to reverse a number using Type Casting in Python.
What is Type Casting in Python
The process of changing a variable's data type is called type casting. It is also referred to as type conversion. If you need to conduct actions that need specific data types, type casting in Python can help you change the data type of a variable. For instance, to execute arithmetic operations on a string of numbers, you may wish to convert it to an integer or a float.
Type casting — a basic programming concept — is widely applied in various contexts to guarantee that data is in the right format for processing. Variables can be converted between different data types using built-in type casting procedures in Python, such as int(), float(), str(), and so on.
Steps for Reversing a Number using Type Casting
Here is a step-by-step process for reversing a number using type casting in Python.
Typecast the Number to a String
The first step is to use the str() function to convert the integer/float number to a string. This function takes a number as a parameter. As a result, we can now handle the number like a string of characters.
str_num = str(num)
Reverse the String
To reverse the number's string representation, we use the String slicing method. The characters are then put in a new string in the opposite sequence.
rev_str = str_num[::-1]
Typecast String to Number
lastly, use the int() function to turn the reversed string back into an integer. By doing this, an integer is created from the inverted string of digits.
rev_num = int(rev_str)
Examples of Reversing a Number Using Type Casting
Now, let us see a few different examples to see how we can reverse a number using the type casting in Python.
Integer Value
In this example, we will take an integer value and then convert it to a string using the str() function. After reversing the string by the slicing method, we will convert the string back into an integer value using the int() function.
Python
# integer number to be reversed
num = 12345
# convert the number to a string
# and reverse it using slicing
reversed_number = int(str(num)[::-1])
# print the reversed number
print("The reverse of", num, "is", reversed_number)
Output:
The reverse of 12345 is 54321
Float Value
In this example, we will take a float point value and then convert it to a string using the str() function. After reversing the string by the slicing method, we will convert the string back into the float value using the float() function.
Python
# float number to be reversed
num = 123.45
# convert the number to a string
# and reverse it using slicing
reversed_number = float(str(num)[::-1])
# print the reversed number
print("The reverse of", num, "is", reversed_number)
Output:
The reverse of 123.45 is 54.321
Negative Integer Value
In this example, we will take a negative integer value and use a conditional statement to determine whether the input number, is positive or negative. Assign -1 to the variable sign if num is negative; else, assign 1.
Then convert the number to its absolute value using the typecasting function, which will eliminate the negative sign. Convert this absolute value to the string using the str() function.
After reversing the string by the slicing method, multiply the reversed number with the sign obtained previously and then convert the string back into an integer value using the int() function.
Python
# negative integer number to be reversed
num = -12345
# Extract the sign of the number
sign = -1 if num < 0 else 1
# Convert the absolute value of the number
# to a string and reverse it using slicing
reversed_number = int(str(abs(num))[::-1]) * sign
# Print the reversed number
print("The reverse of", num, "is", reversed_number)
Output:
The reverse of -12345 is -54321
Conclusion
Reversing a number using type casting in Python is a straightforward process that combines the flexibility of string manipulation with the power of type conversion. This method not only simplifies the implementation but also provides an opportunity to understand and utilize typecasting effectively.
Similar Reads
Python Program to Reverse the Content of a File using Stack
Given a file, the task is to change the content in reverse order using Stack, as well as store the lines of that file in reverse order in Python. Examples: Input: 1 2 3 4 5 Output: 5 4 3 2 1 Approach to Python Program to Reverse a Stack Using Recursion Create an empty stack. One by one push every l
2 min read
Python Program to Reverse a Number
We are given a number and our task is to reverse its digits. For example, if the input is 12345 then the output should be 54321. In this article, we will explore various techniques for reversing a number in Python. Using String SlicingIn this example, the Python code reverses a given number by conve
3 min read
Python program to calculate square of a given number
The task of calculating the square of a number in Python involves determining the result of multiplying a number by itself. For example, given the number 4, its square is 16 because 4 Ã 4 = 16. Using ** operatorexponentiation operator (**) is the most direct and optimized way to compute powers. Sinc
1 min read
Python Program For Adding 1 To A Number Represented As Linked List
Number is represented in linked list such that each digit corresponds to a node in linked list. Add 1 to it. For example 1999 is represented as (1-> 9-> 9 -> 9) and adding 1 to it should change it to (2->0->0->0) Recommended: Please solve it on "PRACTICE" first, before moving on to
4 min read
Python3 Program to Rotate bits of a number
Bit Rotation: A rotation (or circular shift) is an operation similar to a shift except that the bits that fall off at one end are put back to the other end. In the left rotation, the bits that fall off at the left end are put back at the right end. In the right rotation, the bits that fall off at th
3 min read
Python Program to Reverse a linked list
Given pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes. Examples: Input: head: 1 -> 2 -> 3 -> 4 -> NULLOutput: head: 4 -> 3 -> 2 -> 1 -> NULLExplanation: Reversed Linked List: Input:
3 min read
Python program to convert Base 4 system to binary number
Given a base 4 number N, the task is to write a python program to print its binary equivalent. Conversion Table: Examples: Input : N=11002233 Output : 101000010101111 Explanation : From that conversion table we changed 1 to 01, 2 to 10 ,3 to 11 ,0 to 00.Input : N=321321 Output: 111001111001Method 1:
3 min read
Python Program For Printing Reverse Of A Linked List Without Actually Reversing
Given a linked list, print reverse of it using a recursive function. For example, if the given linked list is 1->2->3->4, then output should be 4->3->2->1.Note that the question is only about printing the reverse. To reverse the list itself see this Difficulty Level: Rookie Algorit
2 min read
Python Program to Convert any Positive Real Number to Binary string
Given any Real Number greater than or equal to zero that is passed in as float, print binary representation of entered real number. Examples: Input: 123.5 Output: 1 1 1 1 0 1 1 . 1 Input: 0.25 Output: .01 Mathematical Logic along with steps done in programming: Any real number is divided into two pa
4 min read
Reverse Multiplication Table Using For loop in Python
A multiplication table of any number can be printed using the For loop in Python. We can also print the multiplication table in reverse order using a for loop in Python. In this article, we will see how we can print the multiplication table in reverse order using a for loop in Python. Example: Input
3 min read