Output in Python

An important of many programs is the display of information. In this activity, we are going to display information in Python using print.

Turn In: Output.py -- explorations using print in Python

Create a new Python file called Output.py and type or copy/paste the code below. After putting in the code, run the code and observe the results. Make sure that you understand what is happening and that the code runs without any errors. Try things on your own to explore additional uses of print.

String Literals and Variables

A string literal is just a string that is displayed as you see it (literally). A string literal can be enclosed in either single or double quotes. For instance, "string literal" or 'string literal'.

A variable is a storage location that can hold one value at a time. It is called variable because the stored value can be changed as needed. Other activities will go into more details on variables. For this activity, we are going to store Bob in the name variable, 45 in the hours variable, and overtime will store true. Put the following into your Output.py file.

name = 'Bob'
hours = 45
overtime = True


Print is followed by parenthesis then inside the parenthesis are the items to print. Try the following ...

print('First line')
print("Another line")

Notice when you run the program that you see the string literal ... First line ... on the first line then a blank line (the print with no string literal) then the phrase Another line. Also notice that we used both single and double quotes in this example. In Python, the quotes are interchangable.

The Python documentation (see link below) shows how print works.

Print -- Python documentation

The documentation shows the following syntax ...

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

The *objects means print will process a list of items, for example multiple string literals as shown below.

print("Now is the time", 'for all good men', "to come to the aid of their country.")

When you run your program with that line, you will see the phrase displayed. The objects can also include variables. Put the line below somewhere below the variables that were provided above. For completeness, those lines are provided below.

name = 'Bob'
hours = 45
overtime = True
print(name, 'worked', hours, 'hours this week and has an overtime status of', overtime)

Run the program and notice that Bob is displayed where we typed in name, 45 where we typed in hours, and True where we typed in overtime. Notice that the variables do not have quotes (because they are not a string literal). Try putting quotes around name, run the program, and notice that the word name is displayed instead of Bob.

Two other parts of print to explore ... the sep and end parameters. When doing comma separated objects, the sep is what is placed between those items. The print syntax above shows the default for sep is a space (if you don't put in the sep, then a space is the separator). To change that, add a sep= then specify what should separate the objects.

print('Yesterday', name, 'came to work', 'and wrote some great code.', sep='...')
print("40+5", hours, "50-5", 23+22, sep=" = ")

When those lines are run, the first line puts three dots between each of the objects (i.e. between Yesterday and Bob) because ... was used as the sep. In the second statement, the separator is an equal sign with a space before and after it. Each of the objects in the list are displayed with an equal sign between them. The objects are a combination of string literals, a variable, and a calculation.

The end parameter lets print know what to do after displaying the objects. The default is end='\n' which means new line (starts a new line before displaying the next print items). Let's try a few examples to explore how end works.

print('One', end='')
print('Two', end='zzz')
print('Three', end='\n\n\n')

Notice that when run One is right beside Two because the end is an empty string. The second print line displays the word Two then ends the line with zzz. Three is displayed right after zzz then the three \n for end displays three new lines (the first one moves to the next line then the next two put in two blank lines).

Wrap Up

Print is used often in many Python programs. You will learn more about print as you progress through learning to code in Python.

Copyright © 2021 Eric Schumm. Permission granted to freely use this in your classroom.