Python f-strings vs .format() vs + for Building Output

f-strings, .format(), and + — and why + between text and a number crashes.

  • Lesson 5 of 5
  • Free
  • Ages 10+
  • 3 hands-on levels
Try this lesson interactivelyReal Python, running in your browser.

The dragon explains

Building output

  1. 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.

  2. 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 3
    print("first")print("second")
    Outputfirst second
  3. The 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 3
    print("first", end="\n")print("second", end="\n")
    Outputfirst second
  4. The dragon: Now the crash. You've joined text and numbers with a comma before. Try joining them with + instead, and Python refuses.

    example.pyPython 3
    print("Gold: " + 50)
  5. 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.

  6. The dragon: The fix: str() turns a number into text, so + has two strings to glue.

    example.pyPython 3
    print("Gold: " + str(50))
    OutputGold: 50
  7. The 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 3
    gold = 50print(f"Gold: {gold}")
    OutputGold: 50
  8. The dragon: f-strings never raise that TypeError, because the {} does the str() conversion for you, silently.

    example.pyPython 3
    print(f"Total: {50}")
    OutputTotal: 50
  9. The dragon: One more tool you'll see in other people's code: .format(). {} marks the gaps, and .format() fills them in order.

    example.pyPython 3
    print("Gold: {}".format(50))
    OutputGold: 50
  10. The 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.

  1. Question 1 of 2

    Why do print("first") and print("second") on their own lines, instead of running together as firstsecond?

    question.pyPython 3
    print("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.

  2. Question 2 of 2

    print("Level: " + 5) — what happens when this runs?

    question.pyPython 3
    print("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. 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. +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 real

Stuck three times? The game offers the answer — unasked.