Python does not allow you to end a line inside such an expression; The simplest workaround is to end the line with a backslash.
def __str__(self): return "Car Type \n"+"mpg: %.1f \n" % self.mpg + \ "hp: %.2f \n" %(self.hp) + "pc: %i \n" %self.pc + \ "unit cost: $%.2f \n" %(self.cost) + "price: $%.2f "%(self.price)
In this case, the backslash must be the last character in the string. Essentially, this means "ignoring the fact that there is a new line." Or, in other words, you are avoiding a new line, as this will usually be a significant break.
You can avoid a meaningful new line with a backslash at any time. That would be stupid, but you could even do
def foo(): return \ 1
so foo() will return 1. If you did not have a backslash, 1 will itself cause a syntax error.
source share