Parentheses and other similar blocks (), [], {}
allow for allow for so-called implicit line joining. By and large whitespace and end-of-line characters don’t have a meaning in this case (except where they’re explicitly required).
If you’ve got some time the following part of the docs covers it (implicit line joining, as opposed to explicit line joining with the \
character)-
https://docs.python.org/2/reference/lexical_analysis.html#implicit-line-joining
So your code is fine though there’s no reason for the newlines or whitespace-
parrot = "norwegian blue"
# This is probably bad style but acceptable syntax
# the new line chars are just ignored
print parrot.upper(
)
# This is also ugly whitespace but acceptable syntax
print parrot.upper( )
For some basic examples of where implicit line joining can be useful see below. Some of these don’t really need to multiple lines but hopefully you can see why it could be useful-
# Sometimes this IS good for style + readability
# Perhaps the arguments to a function are complex expressions
# or otherwise long variable names
values = callmyfunction(
longvargname.attribute1,
longvargname.attribute2,
)
# Single string, but written in a readable way-
easy_read = (
"Some literal concatenation "
"can make a multi-line string "
"easier to read."
) # string literal concatenation
# literal dictionaries in a style similar to json can be quite readable
this_dict = {
"longkeynamethattakesupspace": "everybodysaycheese",
"secondnamethattakesupspace": "flash didn't go off, let's try again",
}
# For more complex comprehensions, you can separate out
# some of the import sections for readability
comp = [
str(element) # expression is very clearly separated here
for element in range(101)
if element % 5 == 0 # as is this conditional
]