← Back to course
Code Crew G6+ / Week 01 / Tuesday
2/6
Week 01 Β· Hello, Mission Control

Tuesday

Making Python speak
// Your first real Python programs
⏱ about 30 min

Tuesday: Making Python speak

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 in quotes is a string

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.

QUOTE MARKS OR NOT?
  • Read the question.
  • Tap your answer.
Which line shows the number 10?
What does print("Crew", "ready") show?
Predict: what does print("ab" * 3) show?
WHY THIS EXERCISEThe * sign with a string repeats it, so "ab" appears three times in a row.
print("Crew:", 2 + 2) shows: Crew: ____
print("-" * 5) shows how many dashes?
StatementTrue 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.?
WHY THIS EXERCISEQuote marks turn text into a string, and commas in print add the spaces for you.
At your computer
1. Open IDLE and click after the >>> prompt.
2. Type print("2 + 3") and press Enter.
3. Type print(2 + 3) and press Enter. Compare the two answers.
4. Type print("=" * 30) and press Enter to draw a banner line.
5. Type print("Fuel:", 80, "percent") and press Enter.
6. Write your own welcome line for Mission Control with print. Add a banner line above and below it.

Joining strings with +

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 + "*")
*----*
PLUS OR COMMA?
  • Read the question.
  • Tap your answer.
What does print("Sky" + "Lab") show?
What does print("Sky", "Lab") show?
Which line shows <==> ?
print("Go" + "Go") shows ____
print("#" * 3 + "!") shows ____
StatementTrue 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.?
WHY THIS EXERCISEPlus joins strings with no gap, while commas inside print put one space between items.
Try it
1. In the shell, make a name badge for Nova using only + and *.
2. Draw a top border with "+" + "-" * 10 + "+".
3. Print a middle line with "|" at both ends and Nova inside.
4. Print the border again. Adjust the spaces until the sides line up.

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.

← Monday