Debugging Your Code with Print Statements
The print function is Python's way of displaying messages on the screen—it is how your code speaks to the user.
Python Speaks Through Output
When a program needs to communicate a result or message to the person running it, Python uses the print function. It displays a message on the screen, making print one of the first tools programmers use to understand what their code is doing.
Hello world!The Shape of a Print Statement
A print statement follows a specific structure. It begins with the word print, followed by parentheses. Inside the parentheses goes the message you want Python to display. The message must be enclosed in either single quotes or double quotes.
Quotes are syntax that mark where the message starts and ends. They are not part of the displayed message, so they do not appear on the screen.
Tracing What Python Displays
When Python executes a print statement, it recognizes the print function, looks inside the parentheses, finds the message, and sends that message to the screen. The quotation marks guide Python while it reads the code, but they disappear from the output.
What do you think happens?
Before running print('Hello world!'), what will appear on the screen?
Reveal answer
Answer: Hello world!
The text inside the quotes is displayed, but the quotes themselves are syntax and do not appear in the output.
print('Hello world!')
Choosing Quote Styles
| Quote style | Example | Meaning |
|---|---|---|
| Single quotes | print('Hello') | Displays Hello |
| Double quotes | print("Hello") | Displays Hello |
The choice between single and double quotes is mostly a matter of style and convenience. A practical exception occurs when the message contains an apostrophe. Because an apostrophe is the same character as a single quote, use double quotes around that message so Python does not mistake the apostrophe for the end of the message.
It's workingRepairing a Quotation Error
Using single quotes around a message that contains an apostrophe
Python can interpret the apostrophe in It's as the point where the quoted message ends, causing an error.
Fix:
Use double quotes around the message: print("It's working")Expecting quotation marks to appear in the output
Quotation marks identify the message for Python; they are syntax and do not appear on the screen.
Fix:
Predict only the text inside the quotes: Hello
Using Print While Debugging
Print is useful not only for displaying messages to users but also for debugging. When you want to understand what your code is doing, you can use print to trace the flow of the code, display results, and confirm that values are what you expect. At this stage, the essential skill is being able to write the print structure correctly and predict the text it will display.
Write a Python print statement that displays the message It's ready. Choose quotation marks that allow the apostrophe to remain part of the message. Then predict the exact output before running the statement.
Hints
- The message must go inside the parentheses.
- Use double quotes around a message containing an apostrophe.
- The quotation marks will not appear in the output.
It's readyKey Takeaways
- A Python print statement begins with print and uses parentheses around the message.
- The message inside the parentheses must be enclosed in single quotes or double quotes.
- Quotes mark the message boundaries but do not appear in the output.
- Single and double quotes work identically for ordinary messages.
- Use double quotes when the message contains an apostrophe, or escape the apostrophe when using single quotes.
- Print statements help programmers display results and understand what their code is doing.
Key Takeaways
- A print statement uses the structure print('your message here').
- The message belongs inside parentheses and must be surrounded by matching single or double quotes.
- Quotation marks are Python syntax, so they guide Python but do not appear on the screen.
- Use double quotes for messages containing apostrophes, or escape the apostrophe when using single quotes.
- Before running a print statement, predict the output by reading only the text inside its quotes.