← Back to course
Code Crew G4 / Week 01 / Thursday
4/6
Week 01 Β· Values and print

Thursday

Joining strings
// Real Python, one line at a time
⏱ about 15 min

Thursday: Joining strings

Rocket is making name signs for the docking hatches. He stores "Moon" in one variable and "Dock" in another. Then he joins them with a plus sign. The sign reads MoonDock, all squished together.

"Let's see what happens with numbers," Rocket says. He joins the string "3" and the string "4". The screen shows 34, not 7!

Raven tilts her head. "What do you notice about the quote marks?" she asks. Nova glows. "I found a pattern. Strings and numbers follow different rules."

Plus joins strings

When both sides of + are strings, Python glues them together. It does not add a space unless you give it one.

first = "Moon"
second = "Dock"
print(first + second)
print(first + " " + second)
print("3" + "4")
print(3 + 4)
LineThe screen shows
print(first + second)MoonDock
print(first + " " + second)Moon Dock
print("3" + "4")34
print(3 + 4)7
  • Numbers with + get added.
  • Strings with + get joined.
  • " " is a string that holds one space.
  • "3" in quotes is a string, even though it looks like a number.
a = "Star"
b = "light"
print(a + b)
What word does the program above show?
WHY THIS EXERCISEJoining strings is how programs build names, labels and messages.
LineWhat someone says it showsTrue or false?
print("5" + "5")10?
print(5 + 5)10?
print("Bay" + "2")Bay2?
print("Go" + " " + "now")Gonow?
WHY THIS EXERCISEQuote marks decide whether + adds or joins. Reading them carefully stops a common bug.
FILL THE GAP
  • Read the question.
  • Tap your answer.
Rocket writes print("Dock" + ____ + "7"). He wants the screen to show Dock 7. What goes in the gap?
What does print("10" + "2") show?
Try it
Pick two short words, like sun and set.
Write them as strings joined with +.
Say what the screen would show.

Practice lab: add or join?

planet = "Mars"
base = "Camp"
print(planet + base)
print(base + " " + planet)
print("1" + "5")
print(1 + 5)
What does line 3 show?
What does line 4 show?
What does line 5 show?
What does line 6 show?
PREDICT THE SCREEN
  • Read the question.
  • Tap your answer.
What does print("Sky" + "lab") show?
What does print("2" + "2") show?
Which one joins Moon and Base with a space between them?
LineWhat someone says it showsTrue or false?
print("8" + "1")81?
print(8 + 1)81?
print("Hi" + " " + "Nova")Hi Nova?
print("Ten" + "4")Ten 4?
WHY THIS EXERCISECareful reading of quote marks and spaces stops many output bugs.
What word does print("Rock" + "et") show?
WHY THIS EXERCISEJoining small strings is how programs build bigger words and messages.

Good thinking! You know that quote marks change what + does. Tomorrow you will build a full fuel report.

← Wednesday