We have already seen a lot of output in the previous exercises, but this is not how Python programs would normally behave. If we were to write expressions in a Python file and run it, we would see no output at all. In order for your more complex programs to output anything you will have to use the built-in function #\color{#8959A8} {\mathtt{\text{print}}}#.
A built-in function is a function that is directly built in the Python source code and thus immediately available to the programmer.
Built-in functions are provided by the Python developers because they generally perform some action that is used extensively by developers, like displaying data. We will gradually see more and more built-in functions throughout this course, but if you want to already take a look at all the built-in functions Python 3 provides, visit the official documentation.
The #\color{#8959A8} {\mathtt{\text{print}}}# function will likely be the built-in function you will use the most in your Python endeavor. While there are other methods to display output, the #\color{#8959A8} {\mathtt{\text{print}}}# function is the most concise way to have your Python programs output anything. It is defined as follows:
\[\color{#8959A8} {\mathtt{print}}(\textit{object}_\textit{1}, \textit{object}_\textit{2}, \text{. . .} )\]
Where the #\textit{object}# arguments can be of any Python type, such as numbers, strings or variables.
The complete definition of the #\color{#8959A8} {\mathtt{\text{print}}}# function is actually as follows:
\[\color{#8959A8} {\mathtt{print}}(\textit{object}_\textit{1}, \textit{object}_\textit{2}, \text{. . .}, \mathtt{\text{sep}\color{#3e999f} {\text{=}}\color{#718c00}{\text{" "}}}, \mathtt{\text{end}\color{#3e999f} {\text{=}}\color{#718c00} {\text{"}}\color{#F5871F}{\text{\n}}\color{#718c00} {\text{"}}}, \mathtt{\text{file}\color{#3e999f} {\text{=}}\text{sys.}\color{#4271ae}{\text{stdout}}}, \mathtt{\text{flush}\color{#3e999f} {\text{=}}\color{#F5871F}{\text{False}}} )\]
It provides four additional optional arguments in #\mathtt{\text{sep}}#, #\mathtt{\text{end}}#, #\mathtt{\text{file}}# and #\mathtt{\text{flush}}#. These arguments have default values specified after #\mathtt{\text{=}}# per the definition of the function. The default values can be modified to achieve different behaviour, #\mathtt{\text{sep}}# specifies the string used to separate the resulting strings and #\mathtt{\text{end}}# specifies the string appended to the end of the line, they are set to space #\color{#718c00} {\mathtt{\text{" "}}}# and the newline character #\mathtt{\color{#718c00} {\text{"}}\color{#F5871F} {\text{\n}}\color{#718c00} {\text{"}}}# by default. The function of the #\mathtt{\text{file}}# and #\mathtt{\text{flush}}# arguments is beyond the scope of this course.
The #\color{#8959A8} {\mathtt{\text{print}}}# function converts its arguments to #\color{#4271ae} {\mathtt{\text{str}}}# objects and prints them on the same line separated by spaces. The #\color{#8959A8} {\mathtt{\text{print}}}# function automatically forces a new line after all the arguments are printed. This is accomplished by appending the newline character #\color{#F5871F} {\mathtt{\text{\n}}}# to the end of the line. When no arguments are provided, only the newline character is printed. The newline character is an example of an escape sequence.
An escape sequence is a sequence of one or multiple characters preceded by a backslash that is not evaluated literally when printed.
In addition to the newline character, other potentially useful escape sequences include:
#\mathtt{\text{\t}}#
#\mathtt{\text{\b}}#
#\mathtt{\text{\"}}#
#\mathtt{\text{\'}}#
#\mathtt{\text{\\}\text{\\}}#
Evaluates to ASCII tab.
Evaluates to ASCII backspace.
Evaluates to literal #\mathtt{\text{"}}#.
Evaluates to literal #\mathtt{\text{'}}#.
Evaluates to literal #\mathtt{\text{\\}}#.
>>> print("\tA tab.\n\t\tTwo tabs.")
|
A tab. Two tabs.
|
>>> print("He's a \"developer\".")
|
He's a "developer".
|
>>> print('He\'s a "developer".')
|
He's a "developer".
|
Let's take a look at some examples of #\color{#8959A8} {\mathtt{\text{print}}}# function usage:
Calling #\color{#8959A8} {\mathtt{\text{print}}}# with a single #\color{#4271ae} {\mathtt{\text{str}}}# object as argument:
>>> print("Hello!")
|
Hello!
|
Calling #\color{#8959A8} {\mathtt{\text{print}}}# with a #\color{#4271ae} {\mathtt{\text{str}}}# and an #\color{#4271ae} {\mathtt{\text{int}}}# object as arguments:
>>> print("The answer?", 44)
|
The answer? 44
|
Calling #\color{#8959A8} {\mathtt{\text{print}}}# with a predefined variable:
>>> var = "eggs" >>> print(var)
|
eggs
|
Using the optional #\mathtt{\text{sep}}# argument we can achieve more complex formatting, like so:
>>> print(1, 2, 3, sep=", ")
|
1, 2, 3
|
Or so:
>>> print("First", "Second", "Third", sep="\n")
|
First Second
Third
|
Additionally, we can use the optional #\mathtt{\text{end}}# argument to control what is appended to the end of the output of a single #\color{#8959A8} {\mathtt{\text{print}}}# call:
>>> print("A sentence", "Another sentence", sep=". ", end=".\nThe end.\n")
|
A sentence. Another sentence. The end.
|
Combining the #\color{#8959A8} {\mathtt{\text{print}}}# function with the previously discussed string formatting techniques is a very useful way to preserve the readability of your code in addition to the readability of its output.