Python's Reserved Words and Syntax
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 print
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 it one of the first tools programmers use to share results and understand what their code is doing.
What do you think happens?
What exact text will appear on the screen when this line runs?
Reveal answer
Answer: Hello world!
The text inside the quotes is the message. The quotes mark the message boundaries in the source code, but they do not appear in the output.
Hello world!The Shape of a print Statement
A print statement has a specific structure. It begins with the word print, followed by parentheses. Inside the parentheses is the message you want to display. That message must be enclosed in either single quotes or double quotes.
The parentheses identify what print should display. The quotes identify where the message begins and ends. Both parts are required for this form of a print statement.
Reading a print Statement
Explain what happens when Python runs print("Welcome!").
Find the function: Python reads print and recognizes that the program should display something.
Inspect the parentheses: Python looks inside the parentheses to find the item that should be displayed.
Read the message: The text Welcome! is enclosed in double quotes, so Python can identify its boundaries.
Display the result: Python sends the message to the screen. The quote characters are syntax and do not appear in the output.
The screen displays Welcome!
Quotes and Message Boundaries
Quotes are not part of the message that the user sees. They are syntax: marks in the code that tell Python where the message starts and stops. Python allows either single quotes or double quotes around the message, and both choices work identically when the message does not create a conflict with the chosen quote mark.
| Code | Quote choice | Screen output |
|---|---|---|
| print('Hello') | Single quotes | Hello |
| print("Hello") | Double quotes | Hello |
From Source Code to Screen
When Python executes a print line, it reads the print function, recognizes that something should be displayed, looks inside the parentheses, and sends the message to the screen. The quotes are used while Python interprets the code, but they disappear from the displayed result.
What do you think happens?
Before running this line, predict the exact output: print("Python speaks")
Reveal answer
Answer: Python speaks
The output is the message inside the quotes. Neither the quotes nor the print function name appears on the screen.
Mistakes Beginners Make
Leaving out the opening or closing quote
The quotes mark the boundaries of the message. With one quote missing, Python cannot identify where the message ends.
Fix:
print("Hello")Leaving out a parenthesis
A print statement uses parentheses around the message. The opening and closing parentheses form the required structure.
Fix:
print("Hello")Using a single quote around a message that contains an apostrophe
The apostrophe in It's is the same character as a single quote. Python can interpret it as the end of the message, so the message boundaries become confusing.
Fix:
print("It's ready")
When checking a print statement, inspect it from the outside inward: first look for print, then matching parentheses, then matching quotes around the complete message. If the message contains an apostrophe, choose double quotes or escape the apostrophe with a backslash.
Practice Before Running
Predict the exact screen output for each statement, then check your reasoning by running it.
Hints
- Read only the message between the matching quotes.
- Do not include the quote characters in your predicted output.
- Check whether an apostrophe requires double quotes or an escaped apostrophe.
Fix this statement so it can display the message It's ready: print('It's ready')
Hints
- The apostrophe is inside the message.
- Try surrounding the entire message with double quotes.
Key Takeaways
- A print statement begins with print, uses parentheses, and places the message inside quotes.
- Quotes mark the beginning and end of the message, but they do not appear in the output.
- Single quotes 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.
- To predict the output, read the text inside the quotes and leave the quote characters out.
Key Takeaways
- The print function displays messages on the screen.
- A basic print statement contains print, parentheses, and a quoted message.
- Quotes define message boundaries and are not displayed as part of the output.
- Single and double quotes are both valid, but double quotes are useful when the message contains an apostrophe.
- Missing quotes, missing parentheses, and conflicting apostrophes can make a print statement syntactically incorrect.