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!
Use the list() class to split a string into a list of strings. Use a list comprehension to split a string into a list of integers.
You can have a string split across multiple lines by enclosing it in triple quotes. Alternatively, brackets can also be used to spread a string into different lines. Moreover, backslash works as a line continuation character in Python. You can use it to join text on separate lines and create a multiline string.
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With