For other people to know what we are thinking about, we have to say what’s on our mind! Similarly, we can’t read the computer’s “mind.” We have to tell it to communicate with us. When the computer gives us information, we call it output. Output can include words on the screen, pictures, and sound, among other things.
In this lesson, we’ll learn how to tell Python to write words on the screen. Why do we need to know this? Because getting feedback from our programs lets us know if they’re working correctly and lets the user know what’s going on.
Python Basic Output 1
Run the code in the example below:
- Question: Based on the output we see on the right side, what must
print
be doing? - Answer: It takes what is between the parentheses and outputs it on the right side.
- Question: Why didn’t it output the quotes?
- Take away the quotes and run the code again.
- Answer: Python uses the quotes to understand what type of data it is dealing with. It needs them in this case.
Python Basic Output 2
Change the code so it outputs the following, running it for each item:
- Your name
print("Darth Vader")
- That was interesting
print("That was interesting")
- That was “interesting”
print("That was "interesting"")
- Question: Why the error?
- Answer: Some characters are special and need to be escaped to be printed exactly as they are. In this case, we need to use a backslash (
\
) to escape each quotation mark. Run the following code to make it work:print("That was
\
"interesting
\
"")
Python Basic Output 3
Each print statement outputs on its own line. Run the following:
print("The sentence below is true.")
print("The sentence above is false.")
Python Basic Output 4
Activity
Use your new Python print statement skills to output the good boy below:
^..^ /
/_/\_____/
/\ /\
/ \ / \
- Hint: Remember that each print statement outputs onto a new line.
- Hint: You may have to escape a few characters!
Python Basic Output 5: Ideal Answer
The ideal solution to this is:
print("^..^ /")
print("/_/\_____/")
print(" /\ /\\")
print(" / \ / \\")
The last two lines each contain an extra backslash to escape the backslash after it. Without the extra backslash, Python doesn’t know where the print statement ends!