hello i am reading this python book and one of the exercises says:
Write a function named right_justify that takes a string named s as a parameter and prints the string with enough leading spaces so that the last letter of the string is in column 70 of the display.
ok so i have the following code that prints 70 spaces and the string ‘allen’
def right_justify(s):
print s
right_justify(' ' * 70 + 'Allen')
but when i try to subtract the number of spaces from the string ‘Allen’
sub = len('allen')
def right_justify(s):
print s
right_justify(' ' * 70 - sub + 'Allen')
i get:
“unsupported operand type(s) for -: ‘str’ and ‘int'”
why does it work without the sub variable and it doesn’t with it?
I have checked the type of the sub and it comes out as an int.
You need parentheses:
Your code is evaluated as:
That doesn’t work because you can’t subtract an int from a string.