Formatting the opposite of some numbers, with decimal alignment
There are only really two things you need here.


float. Which allows you to convert the input to a floating point number. If you change to needing more precision or larger numbers, decimal would be a better choice - thanks @200_success. And,
str.format. Which uses the Format String Syntax. Which you can use pad the left with spaces, so the output has a width of eight. {: &gt;8}. This however needs to be adjusted for your "there always are 3 numbers in the decimal" requirement, and so you can force this too with {: &gt;8.3f}.


And so I'd use:

def opposite(x):
    return '{: &gt;8.3f}'.format(-float(x))




If however you don't want to use float then you can use str.lstrip. With just one if-else:

def opposite(x):
    x = x.lstrip()
    if x.startswith('-'):
        x = x[1:]
    else:
        x = '-' + x
    return '{: &gt;8}'.format(x)