The print function


The print function

So far, we were only able to show things in the screen if we did something like this:

x = 'This is a string'
'This is a string'

This doesn’t work in an actual Python program, however. It only works in a Jupyter notebook. But even in Jupyter notebooks, if you wanted to show more than one thing, this doesn’t work. See this:

y = 'This is another string'
'This is another string'

Only y was displayed. To display x and then y, you need to use the print() function. Here it is:

This is a string
This is another string

Just like formating, there are a lot of details in print(). Here, we will be only covering the very basics. Let’s start by printing some numerical variables:

a = 123
b = 98.2082

You can also print the variables one after the other:

print(a, b)
123 98.2082

Notice that print adds an empty space between the arguments that you want to print. There is no limit to how many arguments print can have. The following also works:

print(a, b, x, y, 'and some more')
123 98.2082 This is a string This is another string and some more

Of course when you print numerics, you probably want to do it like this:

print('a={0:d}, b={1:1.2f}'.format(a,b))
a=123, b=98.21

That is, instead of just printing a and then b, you make a string with just the desired format and you print that string.

Notice that print() puts a newline each time it is called. See this:

print('This is the first line.')
print('This will appear in the second line.')
This is the first line.
This will appear in the second line.

There is a way, a special character called \n which can be used to mark a newline in a string. When print() sees this character, then it adds a newline. See how it works:

print('This is the first line.\nThis will appear in the second line.')
This is the first line.
This will appear in the second line.

Here, let me just print 5 new lines:


Characters like \n are called special characters. In this class, we will use the \t character very often to align what we print. This is called the tab character. Here is what it does:


Get it? Here is why it is useful. Let’s print a table of some fake data:

print('ID\tTime (s)\tMass (kg)\t')
print('-' * 40) # Prints 40 '-'
print('{0:d}\t{1:1.2f}\t\t{2:1.2f}'.format(0, 0.01, 12.2))
print('{0:d}\t{1:1.2f}\t\t{2:1.2f}'.format(1, 0.02, 12.5))
print('{0:d}\t{1:1.2f}\t\t{2:1.2f}'.format(2, 0.05, 13.1))
print('{0:d}\t{1:1.2f}\t\t{2:1.2f}'.format(3, 0.07, 13.6))
print('{0:d}\t{1:1.2f}\t\t{2:1.2f}'.format(4, 0.08, 14.2))
ID	Time (s)	Mass (kg)	
0	0.01		12.20
1	0.02		12.50
2	0.05		13.10
3	0.07		13.60
4	0.08		14.20

Notice that I had to use two \t’s to allign the mass. It’s probably too much to try to remember all these details. We will see many more examples in the hands-on activities and you will pick up certain tricks.


Here are some data:

data = [23.0, 21.0, 23.0, 23.0, 23.84, 24.52]

Use print() and formating to present the data like this:


etc. Then calculate the average of the data and print it with two significant digits.

# your code here