12
votes

So, here is a snippet of my code:

return "a Parallelogram with side lengths {} and {}, and interior angle 
{}".format(str(self.base), str(self.side), str(self.theta)) 

It goes beyond the 80 chars for good styling in a line, so I did this:

return "a Parallelogram with side lengths {} and {}, and interior angle\
{}".format(str(self.base), str(self.side), str(self.theta)) 

I added the "\" to break up the string, but then there is this huge blank gap when I print it.

How would you split the code without distorting it?

Thanks!

2

2 Answers

21
votes

You can put parenthesis around the whole expression:

return ("a Parallelogram with side lengths {} and {}, and interior "
        "angle {}".format(self.base, self.side, self.theta))

or you could still use \ to continue the expression, just use separate string literals:

return "a Parallelogram with side lengths {} and {}, and interior " \
       "angle {}".format(self.base, self.side, self.theta)

Note that there is no need to put + between the strings; Python automatically joins consecutive string literals into one:

>>> "one string " "and another"
'one string and another'

I prefer parenthesis myself.

The str() calls are redundant; .format() does that for you by default.

1
votes

Don't break the line in between instead use two strings separated by line continuation but best would be to use brackets

return ("a Parallelogram with side lengths {} and {}, and interior angle "
"{}".format(1, 2, 3))