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 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 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 print function is the most concise way to have your Python programs output anything. It is defined as follows:
Where the arguments can be of any Python type, such as numbers, strings or variables.
The complete definition of the
function is actually as follows:
It provides four additional optional arguments in
,
,
and
. These arguments have default values specified after
per the definition of the function. The default values can be modified to achieve different behaviour,
specifies the string used to separate the resulting strings and
specifies the string appended to the end of the line, they are set to space
and the newline character
by default. The function of the
and
arguments is beyond the scope of this course.
The function converts its arguments to objects and prints them on the same line separated by spaces. The function automatically forces a new line after all the arguments are printed. This is accomplished by appending the newline character 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:
Evaluates to ASCII tab.
Evaluates to ASCII backspace.
Evaluates to literal .
Evaluates to literal .
Evaluates to literal .
>>> 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 function usage:
Calling with a single object as argument:
>>> print("Hello!")
|
Hello!
|
Calling with a and an object as arguments:
>>> print("The answer?", 44)
|
The answer? 44
|
Calling with a predefined variable:
>>> var = "eggs" >>> print(var)
|
eggs
|
Using the optional 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 argument to control what is appended to the end of the output of a single call:
>>> print("A sentence", "Another sentence", sep=". ", end=".\nThe end.\n")
|
A sentence. Another sentence. The end.
|
Combining the 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.