Formatting

This isn’t for a specific exercise, but instead Python 2 in general. I accidentally pressed enter while typing the parentheses for upper() and then pressed ctrl + enter to submit the code, and I didn’t realize the mistake until it was too late. However, the lesson said that this was fine. Why is this?

parrot = "norwegian blue"

print parrot.upper(
)

(I meant to type this:)

parrot = "norwegian blue"

print parrot.upper()

If I’m correct, there should be something that tells when the line of code ends. There was also nothing in between the parentheses, so whatever you are using to run the code probably read no space. I don’t know anything about python though. I’m just using knowledge from other languages.

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
]