Rocket wants a welcome message on the big screen for every crew member who walks into Mission Control. He types quickly, presses Enter, and the screen shows 5 instead of his message.
"Hmm," he says, scratching his head. "I typed print(2 + 3). I thought it would show the whole thing."
Raven leans in and compares two lines on the screen. "What do you notice?" she asks. "This line has quote marks around the words. That line does not."
Nova glows softly. "Interesting," she says. "Quote marks change how Python reads what is inside them. Try both and compare."
Rocket grins and types the line again, this time with quote marks.
Text inside quote marks is called a string. Python shows a string exactly as you wrote it, without doing any math.
Without quote marks, Python works out the value first, then shows the answer. That is why print(2 + 3) shows 5 but print("2 + 3") shows 2 + 3.
You can use double quotes or single quotes, as long as both ends match. Pick double quotes when your text has an apostrophe in it, like "Let's go".
>>> print("2 + 3")
2 + 3
>>> print(2 + 3)
5
>>> print("Fuel:", 80, "percent")
Fuel: 80 percent
>>> print("=" * 12)
============
>>> print("Raven says \"Look closely.\"")
Raven says "Look closely." Commas inside print separate items. Python puts one space between each item, so you do not need to add spaces yourself.
A string times a number repeats the string. "=" * 12 makes a line of twelve equals signs, which is handy for drawing banners.
The last example uses \" to put a quote mark inside a string. The backslash tells Python that this quote mark is part of the text.
| Statement | True or false? |
|---|---|
| print("7 * 2") shows 14. | ? |
| print(7 * 2) shows 14. | ? |
| Both ends of a string must use the same kind of quote mark. | ? |
| You must type a space before each comma inside print. | ? |
A plus sign between two strings joins them into one string. Python adds no space, so you add any space yourself inside the quotes.
You can mix + and * in one line. Python repeats first and joins after, the same order as with numbers.
>>> print("Mission" + "Control")
MissionControl
>>> print("Mission " + "Control")
Mission Control
>>> print("Mission", "Control")
Mission Control
>>> print("*" + "-" * 4 + "*")
*----* | Statement | True or false? |
|---|---|
| print("Rocket" + "Raven") shows a space between the names. | ? |
| print("Rocket", "Raven") shows a space between the names. | ? |
| print("ab" + "cd") shows abcd. | ? |
| print("=" * 3 + "=") shows four equals signs. | ? |
You can now make Python show text, numbers and banners. Tomorrow you will save your lines in a file so you can run them again and again.