Step 1 of 7 · Reading · ~4 min
Learn
Getting Started
Your First Program
By the end of this lesson you can make Python put exact text on the screen: several lines, a blank line, quotation marks inside quotation marks, and backslashes that survive to the output. Getting text out of a program is the first half of every program you will ever write.
print puts text on standard output
print is a built-in function — a named piece of work that Python already knows how to do. You call a function by writing its name followed by parentheses, and whatever you put inside the parentheses is the argument you hand it.
Cappuccino
Three things have to be right in that one line:
- The name is
print, lowercase.PrintandPRINTare different names, and Python does not know them:NameError: name 'Print' is not defined. - The parentheses are what actually calls the function.
print "Cappuccino"was legal in Python 2 and is aSyntaxErrorin Python 3. - The quotes mark where the text starts and stops. They are not part of the message.
Python runs a file top to bottom, and each print ends its own line, so three calls make three lines.
Quotes, and text that contains quotes
Single and double quotes are interchangeable in Python, so 'Table 4' and "Table 4" are the same string. That choice starts to matter when the text itself contains a quote:
the barista's apron
she wrote "decaf" on the cup
Each line uses the other kind of quote on the outside, so the inner one is ordinary text. Use the same kind and the string ends early:
SyntaxError: invalid syntax
Python read 'the barista', then found a stray s apron' it could not make sense of.
Escape sequences: a backslash changes the next character
The other way to put a quote inside a string is a backslash. Inside a string, a backslash and the character after it are read as a single special character:
| You write | You get |
|---|---|
\n | a newline |
\t | a tab |
\" | a literal double quote |
\' | a literal apostrophe |
\\ | one literal backslash |
Bean Origin
she wrote "decaf" on the cup
This is also the most common way a beginner's output goes wrong. A Windows path looks harmless until Python reads the backslashes:
D:
otes able.csv
The \n became a newline and the \t became a tab, eating the n and the t with them. To get a real backslash in the output, double it:
D:\notes\table.csv
# starts a comment — unless it is inside a string
Outside a string, # means "ignore the rest of this line". Comments are notes for people, not instructions for Python:
Order #7
The # inside the quotes is just a character. Only a # outside a string starts a comment.
Blank lines
Calling print with nothing between the parentheses prints an empty line:
top
bottom
print(" ") looks the same on screen but is not the same: that line holds one space character. Graders compare output character by character, and a stray space fails.
Common mistakes
- Capitalising the name —
Print("hi")— givesNameError: name 'Print' is not defined. The function isprint, all lowercase, always. - Writing a path with single backslashes.
"C:\notes"contains a newline, not a backslash, so the output breaks across two lines. Double every backslash you want to keep. - Closing a string with the same quote it contains, which ends it early and gives
SyntaxError: invalid syntax. Switch the outer quotes, or escape the inner one with\. - Making a blank line with
print(" ")instead ofprint(). The space is a real character and the exact comparison fails on it.
Your exercise
Print the four lines shown in the exercise, exactly. The starter prints one placeholder line so you can see the program run; replace it and add the rest. Your program reads nothing — some tests feed text on standard input to check that you ignore it, so do not call input(), which would crash with EOFError on the tests that supply nothing.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…