The dragon explains
Building output
The dragon: You already know a comma inside print() joins words and numbers with a space between. Let's look at three more ways to build a line — and one crash worth understanding.
The dragon: First, a fact you already know, explained properly: every print() call ends with a hidden newline — that IS the end setting you met before, just left at its default.
example.pyPython 3print("first")print("second")
Outputfirst secondThe dragon: print's end defaults to "\n" — a newline character — even when you never type it. That's the whole reason two prints land on two lines: each one signs off with its own line break.
example.pyPython 3print("first", end="\n")print("second", end="\n")
Outputfirst secondThe dragon: Now the crash. You've joined text and numbers with a comma before. Try joining them with + instead, and Python refuses.
example.pyPython 3print("Gold: " + 50)The dragon: That raises a TypeError: can only concatenate str (not "int") to str. + glues text to text — it has no idea how to glue text to a number.
The dragon: The fix: str() turns a number into text, so + has two strings to glue.
example.pyPython 3print("Gold: " + str(50))OutputGold: 50The dragon: There's a cleaner way that skips the problem entirely: an f-string. Put f before the quotes, and anything inside {} gets swapped in automatically — numbers included.
example.pyPython 3gold = 50print(f"Gold: {gold}")
OutputGold: 50The dragon: f-strings never raise that TypeError, because the {} does the str() conversion for you, silently.
example.pyPython 3print(f"Total: {50}")OutputTotal: 50The dragon: One more tool you'll see in other people's code: .format(). {} marks the gaps, and .format() fills them in order.
example.pyPython 3print("Gold: {}".format(50))OutputGold: 50The dragon: Four tools, one job: comma, +, f-string, .format(). The comma and f-string are what you'll reach for most — + needs str() by hand, and .format() you'll mostly just recognise.
Quick check
Check your understanding
Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.
Question 1 of 2
Why do print("first") and print("second") on their own lines, instead of running together as firstsecond?
question.pyPython 3print("first")print("second")
- Because print's end defaults to a newline character, added after every callCorrect
- Because the code is written on two separate lines
- Because sep automatically adds a line break between separate print() calls
Why: Every print() call ends with end="\n" by default — a newline character — even though you never typed it. That's the actual mechanism behind something you already knew: each print() makes its own line.
Question 2 of 2
print("Level: " + 5) — what happens when this runs?
question.pyPython 3print("Level: " + 5)- A TypeError — you can't + a string and a number togetherCorrect
- It prints Level: 5
- It prints Level:5 with no space
Why: A TypeError: can only concatenate str (not "int") to str. + glues text to text only — to fix it, wrap the number in str() first: "Level: " + str(5).
In the game
What you'll practice
Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.
- 1
GuidedYour first try
The dragon wants one line: You have, then the number 12, then coins — all woven together with an f-string, not a comma. Fill in the blank: put coins (already set to 12) inside the {} where it belongs.
- +2
PracticeTwo more, on your own
Try it yourself — in real Python.
This lesson is part of a free topic: every lesson in it is free, and no card is needed.
Start this lesson for realStuck three times? The game offers the answer — unasked.