print
and an Intro to Single- and Double-Quoted StringsThe built-in print
function displays its argument(s) as a line of text:
In [1]: print('Welcome to Python!')
Welcome to Python!
In this case, the argument 'Welcome to Python!'
is a string—a sequence of characters enclosed in single quotes ('
). Unlike when you evaluate expressions in interactive mode, the text that print
displays here is not preceded by Out[1]
. Also, print
does not display a string’s quotes, though we’ll soon show how to display quotes in strings.
You also may enclose a string in double quotes ("
), as in:
In [2]: print("Welcome to Python!")
Welcome to Python!
Python programmers generally prefer single quotes.
When print
completes its task, it positions the screen cursor at the beginning of the next line. This is similar to what happens when you press the Enter (or Return) key while typing in a text editor.
The print
function can receive a comma-separated list of arguments, as in:
In [3]: print('Welcome', 'to', 'Python!')
Welcome to Python!
The print
function displays each argument separated from the next by a space, producing the same output as in the two preceding snippets. Here we showed a comma-separated list of strings, but the values can be of any type. We’ll show in the next chapter how to prevent automatic spacing between values or use a different separator than space.
When a backslash (\
) appears in a string, it’s known as the escape character. The backslash and the character immediately following it form an escape sequence. For example, \n
represents the newline character escape sequence, which tells print
to move the output cursor to the next line. Placing two newline characters back-to-back displays a blank line. The following snippet uses three newline characters to create many lines of output:
In [4]: print('Welcome\nto\n\nPython!')
Welcome
to
Python!
The following table shows some common escape sequences.
You may also split a long string (or a long statement) over several lines by using the \ continuation character as the last character on a line to ignore the line break:
In [5]: print('this is a longer string, so we \
...: split it over two lines')
this is a longer string, so we split it over two lines
The interpreter reassembles the string’s parts into a single string with no line break. Though the backslash character in the preceding snippet is inside a string, it’s not the escape character because another character does not follow it.
Calculations can be performed in print
statements:
In [6]: print('Sum is', 7 + 3)
Sum is 10
(Fill-In) The function instructs the computer to display information on the screen.
Answer: print
.
(Fill-In) Values of the data type contain a sequence of characters.
Answer: string (type str
).
(IPython Session) Write an expression that displays the type of 'word'
.
Answer:
In [1]: type('word')
Out[1]: str
(IPython Session) What does the following print
statement display?
print('int(5.2)', 'truncates 5.2 to', int(5.2))
Answer:
In [2]: print('int(5.2)', 'truncates 5.2 to', int(5.2))
int(5.2) truncates 5.2 to 5