Prints - multiline
Sometimes we have a lot to say. If we try and fit it all in one print statement, it may be hard to read (in the code atleast). So lets cover how to make multi-line print statements easier to read.
The first thing we can do is separate formation of the string to above the print and store in a variable, then simply print this variable.
In a similar manner to how we concatted strings earlier, we can use a backslash to indicate the line continues onto the next line.
There are also triple quotes we can use, either single or double quotation marks. Just like with regular strings in Python, there is essentially no difference. Notice how in the following example single quotes display without any issue. Double quotes will as well.
The one drawback with triple quotes is needing to write characters up against the left side of the editor to prevent extra spacing.
The triple double-quote is often used to write docstrings. These are very helpful to explain the purpose of a function. This is a very good habit to start, writing docstrings as well as comments throughout our code helps both others understand the purpose of it as well as ourselves one plus years later.
def fuel_warning(fuel_level):
"""
returns a warning if the fuel level is too low
"""
# fuel level can be in range(0, 100) or range(0,1)
# we should convert to one or the other
if 1 < fuel_level <= 100:
fuel_level = fuel_level / 100
if fuel_level < 0.25:
return "fuel is low"
return "fuel is fine"