Published using Google Docs
displaying_output
Updated automatically every 5 minutes

Muzny/Rachlin

DS 2000

Fall 2022

Reference Sheet: using print()

print()

The print() command is used to display output to the user.

It's usage follows:

print(thing that you want to print)

It is extremely important that print is written exactly as print and not Print or pRiNt or any other variation on the word.

Here, thing that you want to print can be any data type (a string, a number, etc).

Example:

code

output

print(7)

print(3 + 127)

print("hello")

7

130

hello

You'll notice that every time the print() command is executed, the next line of output is displayed on the line below what was just printed.

Example:

print("first line")

print("second line")

first line

second line

This is because the print() command adds what's called a newline character to the end of whatever you've asked it to display for you. We'll talk more about this when we get to advanced output display.

To display an empty line, use the print() command without anything between the parentheses (but do make sure to include them).

Example:

print("first line")

print()

print("second line")

first line

second line

Printing multiple items on the same line

To print multiple items on the same line, we can use the print command with commas. What we're doing is we're giving this command multiple things that we want to print, each separated with a comma. (also called arguments).

Example:

print("thing1", "thing2")

print(2, 3)

thing1 thing2

2 3

Compare and contrast the following two lines of code!

print(2 + 3)

print(2, 3)

5

2 3

We can also use this strategy to print both a string and a number on the same line!

print("Felix's favorite number:", 97)

Felix's favorite number: 97