Python Program for Reverse of a Number Using Type Casting
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.
# 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.
# 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.
# 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.
Contact Us