Normalizing Line Endings
4.4 Printing Information
SyntaxError: EOL while scanning string literal
As we saw in Section 4.1, Creating Strings of Characters, on page 65, EOL stands for “end of line”; so in this error report, Python is saying that it reached the end of the line before it found the end of the string.
To span multiple lines, put three single quotes or three double quotes around the string instead of one of each. The string can then span as many lines as you want:
>>> '''one
... two
... three''' 'one\ntwo\nthree'
Notice that the string Python creates contains a \n sequence everywhere our input started a new line. As programmers, we see escape sequences in strings.
In Section 4.4, Printing Information, on page 70, we show that when strings are printed, users see the properly rendered strings rather than the escape sequences. That is, they see a tab or a quote rather than \t or \'.
Normalizing Line Endings
In reality, each of the three major operating systems uses a different set of characters to indicate the end of a line. This set of characters is called a newline. On Linux and Mac OS X, a newline is one '\n' character; on version 9 and earlier of Mac OS, it is one '\r'; and on Windows, the ends of lines are marked with both characters as '\r\n'. Python always uses a single \n to indicate a newline, even on operating systems like Windows that do things other ways. This is called normalizing the string; Python does this so that you can write exactly the same program no matter what kind of machine you’re running on.
4.4 Printing Information
In Section 3.7, Writing and Running a Program, on page 59, built-in function print was used to print values to the screen. We will use print to print messages
to the users of our program. Those messages may include the values that expressions produce and the values that the variables refer to. Here are two examples of printing:
>>> print(1 + 1)
2
>>> print("The Latin 'Oryctolagus cuniculus' means 'domestic rabbit'.")
The Latin 'Oryctolagus cuniculus' means 'domestic rabbit'.
Function print doesn’t allow any styling of the output: no colors, no italics, no boldface. All output is plain text.
The first function call does what you would expect from the numeric examples we have seen previously, but the second does something slightly different from previous string examples: it strips off the quotes around the string and shows us the string’s contents rather than its representation. This example makes the difference between the two even clearer:
>>> print('In 1859, Charles Darwin revolutionized biology')
In 1859, Charles Darwin revolutionized biology
>>> print('and our understanding of ourselves')
and our understanding of ourselves
>>> print('by publishing "On the Origin of Species".')
by publishing "On the Origin of Species".
And the following example shows that when Python prints a string, it prints the values of any escape sequences in the string rather than their backslashed representations:
>>> print('one\ttwo\nthree\tfour')
one two
three four
The example above shows how the tab character \t can be used to lay values out in columns.
In Section 4.3, Creating a Multiline String, on page 70, we saw that \n indicates a new line in multiline strings. When a multiline string is printed, those \n sequences are displayed as new lines:
>>> numbers = '''one
Printing Information
•
71Function print takes a comma-separated list of values to print and prints the values with a single space between them and a newline after the last value:
>>> print(1, 2, 3)
1 2 3
>>>
When called with no arguments, print ends the current line, advancing to the next one:
>>> print()
>>>
Function print can print values of any type, and it can even print values of different types in the same function call:
>>> print(1, 'two', 'three', 4.0)
1 two three 4.0
It is also possible to call print with an expression as an argument. It will print the value of that expression:
>>> radius = 5
>>> print("The diameter of the circle is", radius * 2, "cm.")
The diameter of the circle is 10 cm.
Function print has a few extra helpful features; here is the help documentation for it:
>>> help(print)
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
The parameters sep, end, file, and flush have assignment statements in the function header! These are called default parameter values: by default, if we call function print with a comma-separated list of values, the separator is a space; similarly, a newline character appears at the end of every printed string. (We won’t discuss file and flush; they are beyond the scope of this text.)
We can supply different values by using keyword arguments. (In the Python documentation, these are often referred to explicitly as kwargs.) That’s a fancy term for assigning a value to a parameter name in the function call. Here, we separate each value with a comma and a space instead of just a space by including sep=', ' as an argument:
>>> print('a', 'b', 'c') # The separator is a space by default
a b c
>>> print('a', 'b', 'c', sep=', ')
a, b, c
Often you’ll want to print information but not start a new line. To do this, use the keyword argument end='' to tell Python to end with an empty string instead of a new line:
>>> print('a', 'b', 'c', sep=', ', end='')
a, b, c>>>
Notice how the last prompt appeared right after the 'c'. Typically, end='' is used only in programs, not in the shell. Here is a program that converts three temperatures from Fahrenheit to Celsius and prints using keyword arguments:
def convert_to_celsius(fahrenheit):
""" (number) -> float
Return the number of Celsius degrees equivalent to fahrenheit degrees.
>>> convert_to_celsius(75) 23.88888888888889
"""
return (fahrenheit - 32.0) * 5.0 / 9.0
print('80, 78.8, and 10.4 degrees Fahrenheit are equal to ', end='') print(convert_to_celsius(80), end=', \n')
print(convert_to_celsius(78.8), end=', and ') print(convert_to_celsius(10.4), end=' Celsius.\n')
Here’s the output of running this program:
80, 78.8, and 10.4 degrees Fahrenheit are equal to 26.666666666666668, 26.0, and -12.0 Celsius.