Back to Python Fundamentals

2: Printing Output

print() helps you inspect values and explain what your program is doing.

Contributed by Bijou Raj

In [1]Text cell

Printing is how we get lines to print to our terminal/console output window. We use the print() function to do this.

In [2]Code cellpyodide
In [3]Text cell

We can print anything that has a form to be printed in the terminal, from variables, to objects (which we'll cover later).

In [4]Code cellpyodide
In [5]Code cellpyodide
In [6]Text cell

We can also use one print statement to print out multiple things, as well as multiple lines using the newline \n character.

In [7]Code cellpyodide
In [8]Code cellpyodide
In [9]Text cell

Using the backslash allows us to print out other escape characters too. " will let us print the ", for example, and \t will give us a tab.

In [10]Code cellpyodide
In [11]Code cellpyodide
In [12]Text cell

Try it out! Print the first name on one line, the second name on another line, then both names separated by a space on the third.

In [13]Challenge cellpyodide
In [14]Text cell

We can run into type mismatch sometimes when we're printing variables. This can happen because the print() function only takes in strings, so something like this will not work:

In [15]Code cellpyodide
In [16]Text cell

We can solve this by using wrappers, or by using f strings. These allow us to insert variables directly into our strings.

In [17]Code cellpyodide
In [18]Code cellpyodide
In [19]Text cell

When doing this, always keep in mind that we have to match characters. If we open a string with ", we have to close it with ". The same goes for {}, as well as (). Keeping that in mind, try solving the next few exercises.

In [20]Text cell

Print Suzie's age, followed by a tab, followed by her name.

In [21]Challenge cellpyodide
In [22]Text cell

Print out what i is on each iteration. The first output line should be "i is 0" and so on.

In [23]Challenge cellpyodide
In [24]Text cell

Print out the person's age after each year has passed. The output should say: After (number) years, the person is: (age)

In [25]Challenge cellpyodide