Printing output
Contents
Printing output#
Introduction#
Using code to get a computer to talk back to you is typically the first thing you’ll learn when studying a programming language. Not only is it typically a simple task, and therefore perfect for newcomers, but learning this skill allows for an easy introduction to core coding concepts such as functions, data types, variables, and more.
Following in the footsteps of others, the first programming concept we’ll cover is printing output to the user. This ability is useful for several reasons, which we’ll cover in the examples throughout this introductory section. Since Python uses a function to print output, we’ll also introduce functions and arguments along the way.
Objectives#
After working through this page, you should be able to answer these questions:
How do you display output to the user in Python?
What is a function?
What is an argument?
How can you print multiple statements?
How do you print variable values?
What options keyword arguments can you pass the
print()
function? What do they do?
The print()
function#
To print output in Python, use the print()
function. We’ll cover functions in more detail in a later section. For now, think of a function as a tool in your coding toolbelt that is waiting to be used when you need it. It is a piece of code that performs a specific task, and it will perform that task only when you tell it to do so. (Much like a hammer to a carpenter.) In Python we use, or call, a function by typing it’s name followed by open and closed parentheses. For example,
print('Hello World!')
Hello World!
Definition
A function is a piece of code that performs a specific task, and is run only when called.
The print()
function’s single purpose is outputting, to the user, whatever value it is passed. Most functions accept arguments, or values passed to the function that modify it’s behavior. In the example above we passed the string 'Hello World!'
, so that is what it printed back to you.
Definition
An argument is a value that is passed to a function that can modify the function’s behavior.
If we call print()
without passing arguments, nothing would be output but a new line:
print()
We can string together multiple calls to print()
. Imagine you work for an investments firm and you are desigining an app where clients can sign in and view the value of their portfolio. When a user returns to the app, you might want to update them on how the value of their portfolio has changed since the last time they signed in.
We can do this by calling print()
as many times as necessary to get the user up to speed.
print('Welcome back, Jane!')
print('You last visited 26 days ago.')
print('Since then, the value of your investments has increased 3.4%.')
Welcome back, Jane!
You last visited 26 days ago.
Since then, the value of your investments has increased 3.4%.
Printing multiple arguments#
Continuing our example, imagine that, when a user is signing up, you’d like to inform if their desired username is available. Supposing someone wants user1234
, you could return the following:
print('Congratulations,', 'user1234', 'is available!')
Congratulations, user1234 is available!
Here we passed multiple arguments to print()
. When passed multiple arguments, its default behavior is to return whatever it is passed, separated by a space.
Every argument we’ve passed print()
so far has been surrounded by single quotes. These are called strings, or sequences of unicode characters. In Python there are various data types that are used to represent different types of values. We’ll get into these basic data types in a future lesson.
For now it is sufficient to know that we can pass print()
not only strings (which represent text values), but also other data types like integers (whole numbers) or floats (numbers with decimals).
print('Welcome back, Jane!')
print('You last visited', 26, 'days ago.')
print('Since then, the value of your investments has increased', 3.4, '%.')
Welcome back, Jane!
You last visited 26 days ago.
Since then, the value of your investments has increased 3.4 %.
Definition
A string is a built-in data type that holds a sequence of unicode characters.
Printing variable values#
Looking at the above print statements, in an actual application we’d have code that interacted with a database that would determine the last time Jane visited the app and the change in portfolio value since that visit. After calculating those values they would be stored as variables, or names that point to an object or value.
Definition
A variable is a name that points to an object or value.
We’ll cover variables with more depth soon. For this exercise we’ll show that you can print a variable’s value by passing the variable to the print()
function, and it will know what to do from there.
For example, suppose our application had variables that stored the user’s name (Jane), the number of days since the last visit (26), and the portfolio value change (+3.4%).
user_name = 'Jane'
days_since_last_visit = 26
portfolio_change_percent = 3.4
We can make the previous print statement dynamic (reuseable) by adding these variables as arguments.
print('Welcome back,', user_name, '!')
print('You last visited', days_since_last_visit, 'days ago.')
print('Since then, the value of your investments has increased', portfolio_change_percent, '%.')
Welcome back, Jane !
You last visited 26 days ago.
Since then, the value of your investments has increased 3.4 %.
Now we can reuse these same print statements for all users and only need to modify the variable values elsewhere in the application (something we’ll learn to do later).
Modifying print()
’s default behavior#
Perhaps you noticed that, in lines one and three in the output from our last print()
statement, there is a space between the final word and the punctuation. We wouldn’t want sentences with grammatical errors being displayed to users of our application, so we’ll learn how to fix that next.
Changing what separates each argument#
As we stated earlier, print()
’s default behavior is to take whatever arguments you pass it, separate those arguments with a space, and print them back to you, ending with a new line. What if we wanted to separate the arguments by something other than a space? Or we didn’t want a new line to begin following the output?
Coding tip
An essential tool in every coder’s toolbelt is knowing how to use a search engine to get answers to your programming questions.
If we Google python print change what separates arguments, the first result that displays as of this writing is a codingem article titled Python print() Function Parameters Explained. Near the top of the article we learn that the print()
function accepts an argument named sep
, which has a default value of ' '
.
If we modify this keyword argument (a named argument that comes with a default value) we can change what separates the values of the various arguments we pass print()
.
Definition
A keyword argument is a named argument, which holds a default value, that can be used when calling a function.
If we go back to the last thing we printed and add sep=' '
to the arguments we’re passing:
print('Welcome back,', user_name, '!', sep=' ')
print('You last visited', days_since_last_visit, 'days ago.', sep=' ')
print('Since then, the value of your investments has increased', portfolio_change_percent, '%.', sep=' ')
Welcome back, Jane !
You last visited 26 days ago.
Since then, the value of your investments has increased 3.4 %.
We get the same output! That is because, if we don’t specify an alternative value for sep
, the print()
function behaves as though we did pass sep=' '
. We’ll learn a lot more about keyword arguments in a later lesson.
To fix the issue we’re having here, we can specify that we don’t want spaces between arguments. If we set sep=''
(an empty string), we get:
print('Welcome back,', user_name, '!', sep='')
print('You last visited', days_since_last_visit, 'days ago.', sep='')
print('Since then, the value of your investments has increased', portfolio_change_percent, '%.', sep='')
Welcome back,Jane!
You last visited26days ago.
Since then, the value of your investments has increased3.4%.
We no longer have a space between the final word of our sentence and the punctuation! But we introduced a new readability issue. sep=''
removed spaces between all our arguments. To fix this problem we can manually add spaces in our strings so the text displays properly.
print('Welcome back, ', user_name, '!', sep='')
print('You last visited ', days_since_last_visit, ' days ago.', sep='')
print('Since then, the value of your investments has increased ', portfolio_change_percent, '%.', sep='')
Welcome back, Jane!
You last visited 26 days ago.
Since then, the value of your investments has increased 3.4%.
Perfect!
Changing what follows the output#
The codingem article also discusses the end
keyword argument. end
dictates what print()
will output after it has printed the values of all the arguments. It’s default value is '\n'
, which is Python’s character for a new line.
To reinforce what is meant by a default value, note that the output of these two print()
calls is the same.
print('Welcome back, ', user_name, '!', sep='', end='\n')
print('Welcome back, ', user_name, '!', sep='')
Welcome back, Jane!
Welcome back, Jane!
An important thing to note here. The print()
function can handle changes to the default sep
and end
values at the same time. This means we can play with these keyword arguments to meet our needs.
Let’s suppose that, when Jane returns to the application, we want all her details to be displayed on one line (instead of multiple lines). We could do this, using multiple print statements, if we modify end
to be a space instead of a new line. We’ll retain the modifications we made earlier, inserting spaces into the string arguments and keeping sep=''
to maintain readability.
print('Welcome back, ', user_name, '!', sep='', end=' ')
print('You last visited ', days_since_last_visit, ' days ago.', sep='', end=' ')
print('Since then, the value of your investments has increased ', portfolio_change_percent, '%.', sep='', end=' ')
Welcome back, Jane! You last visited 26 days ago. Since then, the value of your investments has increased 3.4%.
Conclusion#
Being able to deliver output to a user is an essential piece to any application. It is also helpful to you, as a programmer, when you are debugging and testing your code (which we’ll cover in a later lesson). Python makes printing output easy with the print()
function. It works intuitively, outputting whatever you pass it, even if you pass it multiple arguments.
As we explored print()
’s capabilities, we introduced the concept of functions, which are blocks of code that are standing by to perform a specific task for you. We pass functions arguments, which can take the shape of an on-the-fly value (e.g., print('Hello World!')
), or some variable we’ve defined. print()
handles arguments from all sorts of data types, not just strings.
The print()
function doesn’t require that an argument be passed, but it is more helpful when at least one is. And it has pre-defined keyword arguments called sep
and end
which can be modified to change how the values of arguments are separated and how the output ends, respectively.
In the next section we’ll explore the flip side of printing output: gathering input from the user. Before moving on, test what you’ve learned by answering the questions below and work through the included exercises.
Test questions#
Consider each question below and write an answer based on what you’ve learned. Once you’ve responded to each question, check your knowledge by clicking the dropdown to see a solution.
1. How do you display output to the user in Python?
Use the print()
function! Call the function and pass an argument of any data type, and its value will be output to the user.
2. What is a function?
A function is a piece of code that performs a specific task, and is run only when called.
3. What is an argument?
An argument is a value that is passed to a function that can modify the function’s behavior.
4. How can you print multiple statements?
The print()
function accepts as many arguments as you like! To have it output the values of multiple objects, pass it multiple arguments!
Alternatively, you can call print()
multiple times.
5. How do you print variable values?
Pass the print()
function a variable as an argument, and it will print the variable’s value.
6. What options keyword arguments can you pass the print()
function? What do they do?
print()
accepts the sep
and end
keyword arguments.
sep
determines how values of multiple arguments will be separated in the output, and defaults to a blank space (' '
).
end
determines what is output after all the argument values. It defaults to a new line ('\n'
).
Exercises#
In progress.
Glossary#
An argument is a value that is passed to a function that can modify the function’s behavior.
A function is a piece of code that performs a specific task, and is run only when called.
A keyword argument is a named argument, which holds a default value, that can be used when calling a function.
The string is a built-in data type that holds a sequence of unicode characters.
A variable is a name that points to an object or value.