Victor Jeman Academy
Python Fundamentals10 min

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 f and drops a variable straight into { }
  • Round numbers in a message with :.2f so money and temperatures read cleanly
  • Read a choice from the player and branch on it with if/elif/else

String concatenation

Concatenare vs Adunare
schimba tipul
"3"
+concatenare
"5"
=
"35"
str"3" + "5" lipeste textele -> "35"

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:

text
Hero Andrew is a legendary Mage!

f-strings

f-string template
f"Jucatorul {nume} a atins nivelul {nivel}!"
Pasul 1: Click pe o casuta colorata din template
Jucatorul a atins nivelul !

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 number 1. The choice came from input(), so it is text.
  • Keep an else branch 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

Ask the player to enter their hero's name using input(). Then display a personalized welcome message: "Welcome to the Python Kingdom, [name]! Your adventure begins now."

Exercise : Interactive class selection

Display the 3 available classes (1. Warrior, 2. Mage, 3. Archer) and ask the player to choose by typing 1, 2, or 3. Use if/elif/else to set the variable hero_class to the correct value. If the player types something else, set the class to "Warrior" as a default value. Display the chosen class.

Exercise : Hero Sheet with f-strings

After the player has chosen their name and class, display a nicely formatted character sheet using f-strings. The sheet must include: name, class, HP, attack, defense, and gold. Use = to create a frame and align the values. The class bonuses are from the Making decisions lesson (Warrior: +20 HP, +5 attack; Mage: +5 HP, +10 attack; Archer: +7 attack, +5 defense).

Exercise : Interactive shop

Create an interactive shop where the player chooses what to buy. Display 3 items with prices: Iron Sword (30 gold, +8 attack), Wooden Shield (25 gold, +5 defense), HP Potion (15 gold, +20 HP). Ask the player to choose (1/2/3/4 where 4 = buy nothing). Check if they have enough gold, apply the bonus, and display the corresponding message with f-strings.

Exercise : Temperature converter

Build a temperature converter that works in both directions. The program first asks the user what type of conversion they want (type "1" for Celsius to Fahrenheit or "2" for Fahrenheit to Celsius), then asks for the temperature and displays the result.

The formulas are:

  • Celsius to Fahrenheit: F = C * 9/5 + 32
  • Fahrenheit to Celsius: C = (F - 32) * 5/9

Display the result with a descriptive message using f-strings.

Exercise : Tip calculator

Write a program that asks the user for the bill amount and the desired tip percentage. Calculate the tip and the total to pay. Display the result nicely formatted with f-strings, rounded to two decimals with :.2f so the money reads cleanly. For example, for a bill of 85 dollars and a 15% tip: "Bill: 85.00 dollars, Tip (15%): 12.75 dollars, Total: 97.75 dollars".

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.

Question 1 of 3

What does print("3" + "5") display?