Python Breaking Long Lines With \
You can break a long line of Python code into multiple lines using either parentheses () or backslash \. For example:
# Using parentheses
a = (1 + 2 + 3 + 4 - 5 * 2
+ 6 * 3 - 7 * 4)
# Using backslash
b = 1 + 2 + 3 + 4 - \
5 * 2 + \
6 * 3 - \
7 * 4
Both a and b will have the same value. The preferred way of breaking long lines is using parentheses as they are more readable and less error-prone than backslashes. You can also use parentheses to break long strings, if statements, function calls, etc.
One of the biggest reasons a code becomes unreadable is because of the long file address, links, or list elements.
url = 'https://medium.com/pythoneers/10-underrated-python-packages-every-data-scientist-should-know-86b4355cc35e'
You can change the point of wrap with the help of a backslash \
url = 'https://medium.com/pythoneers/'\
'10-underrated-python-packages-every-'\
'data-scientist-should-know-86b4355cc35e'
print(url)
# output https://medium.com/pythoneers/10-underrated-python-packages-every-data-scientist-should-know-86b4355cc35e
If you have any questions about this code, you can drop a line in comment.
Comments
Post a Comment