Concepts / Debugging Your Code with Print Statements

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.

  • Programming

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.

python
Output
Hello world!
executesdisplays messageprint statementprint('Hello world!')print functionreads the messagescreenHello world!
How does the text inside a Python print statement move from the code to the screen?

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.

printfunction name(opens the argument area'message beginsHellomessage text'message ends)closes the argument area
What does each part of print('Hello') contain, and how are the function name, parentheses, quotes, and text connected?

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?

  • Hello world!
  • 'Hello world!'
  • print('Hello world!')
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 styleExampleMeaning
Single quotesprint('Hello')Displays Hello
Double quotesprint("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.

python
Output
It's working

Repairing 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.

EASY

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.
python
Output
It's ready

Key Takeaways

  1. A Python print statement begins with print and uses parentheses around the message.
  2. The message inside the parentheses must be enclosed in single quotes or double quotes.
  3. Quotes mark the message boundaries but do not appear in the output.
  4. Single and double quotes work identically for ordinary messages.
  5. Use double quotes when the message contains an apostrophe, or escape the apostrophe when using single quotes.
  6. 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.