Building strings: concatenation and f-strings
Assemble and format output text with concatenation and f-strings, then react to player input with branching.
What You'll Learn
- Glue strings together with the + operator
- Format messages cleanly with f-strings
- Drop variables and expressions straight into text
- React to player input by branching with if/elif/else
Last lesson you got input from the player and converted it to numbers. Every reply you printed was stitched together by hand with + and a pile of quotes. Now you learn the two real tools for building text: concatenation and f-strings. By the end you will read a choice from the player and answer with a clean, formatted message.
In this lesson, you will be able to:
- Glue strings together with the
+operator (a symbol that performs an action), called concatenation - Build messages with f-strings: text that starts with
fand drops a variable straight into{ } - Round numbers in a message with
:.2fso money and temperatures read cleanly - Read a choice from the player and branch on it with if/elif/else
String concatenation
Concatenation means gluing two or more strings together. You do it with the + operator. This is the place to learn it, you will lean on it for the rest of the course:
Python does not slip in a space for you. If you want one between the words, you add it yourself:
Glue input() together with text and you get a message built around whatever the player typed:
If the player types "Andrew" and "Mage", they see:
Hero Andrew is a legendary Mage!
f-strings
Concatenation with + works, but stack four or five variables and it turns into a mess of quotes, plus signs, and str() calls. f-strings (formatted strings) fix that, and from here on they are the way you build text with variables in it.
An f-string starts with the letter f before the opening quote, and you drop variables straight into the text inside curly braces {}:
Result: Player Andrew has reached level 7!
Compare the two approaches:
You can put whole expressions inside the braces, not just plain variables:
Put it all together into a small chatbot that reads input and answers with f-strings:
The \n inside the f-string starts a new line, so the output is not one cramped block of text.
input() always hands you a string. Want to do math? Convert it first with int() or float(). Building a message with variables in it? Reach for an f-string.
Reacting to what the player types
You already know if/elif/else from the Making decisions lesson, and last lesson you learned to read input. Now you combine them: the thing you are branching on comes from the player instead of being hardcoded. Read a choice with input(), branch on it, and answer with an f-string:
Two things to copy from this:
- Compare against the string
"1", not the number1. The choice came frominput(), so it is text. - Keep an
elsebranch for when the player types something you did not expect. Never assume they will type what you asked for.
That pattern, read a choice then branch on it, is exactly what the exercises below want from you.
Exercise : The hero's name in the Python Kingdom
Exercise : Interactive class selection
Exercise : Hero Sheet with f-strings
Exercise : Interactive shop
Exercise : Temperature converter
Exercise : Tip calculator
Complete game code: Python Kingdom
Now your game has interactive character creation. The player picks their name and class, sees the stats laid out with f-strings, and buys one thing from the shop:
Notice the shop only lets the player buy once. Type the wrong number and the program just ends, no second chance. Real games loop: "What do you want to buy?" over and over until the player walks away. To do that you need a way to repeat code until you say stop. That is while, and it is the next lesson.
Test Your Knowledge
Check how well you understood the lesson with these 3 questions.

