Reverse number

Isabelle Viktoria Maciohsek · Python, Math · Nov 2, 2020

Reverses a number.

  • Use str() to convert the number to a string, slice notation to reverse it and str.replace() to remove the sign.
  • Use float() to convert the result to a number and math.copysign() to copy the original sign.
from math import copysign

def reverse_number(n):
  return copysign(float(str(n)[::-1].replace('-', '')), n)
reverse_number(981) # 189
reverse_number(-500) # -5
reverse_number(73.6) # 6.37
reverse_number(-5.23) # -32.5

Recommended snippets

  • Digitize number

    Python, Math

    Converts a number to a list of digits.

  • Sum of powers

    Python, Math

    Returns the sum of the powers of all the numbers from start to end (both inclusive).

  • Initializes a list containing the numbers in the specified range where start and end are inclusive and the ratio between two terms is step.