📝 Python

Mixing Text with Numbers (and the Magic of f-strings) 🪄

0
Author
04e5cc8b-58ac-4bdc-bdee-661bbb
📅
Published
30.03.2026
⏱️
Reading time
1 min
👁️
Views
122
🌱
Level
Beginner

In programming, we often need to display text alongside numbers or variables. Imagine you’re building a game and need to show the player’s level and score.

💔 Why the Old Way Is Awkward

Traditionally you had to “glue” text and numbers together like building blocks, using commas or plus signs:

name = "Wanderer"
level = 5
gold = 150

print("Hello, " + name + "! Your level: " + str(level) + ", gold: " + str(gold) + " coins.")

Look at all those quotes and plus signs! On top of that, numbers (like level) have to be explicitly converted to strings with str(), otherwise Python raises an error (you can’t just add text and a number). It’s tedious, easy to get confused, and prone to typos.

💖 The Beautiful Way: f-strings!

Starting with Python 3.6, a real piece of magic arrived — f-strings. The f stands for format.

To use them, simply put the letter f (lowercase) before the opening quote. Then write variables directly inside the string, wrapped in curly braces { }.

Look how simple it is:

name = "Wanderer"
level = 5
gold = 150

# The f before the quote makes this string magical!
print(f"Hello, {name}! Your level: {level}, gold: {gold} coins.")

How does it work?

The curly braces {} act like little portals. Python sees them, knows there’s a variable (or even a math expression!) inside, evaluates it, and substitutes the result right into the string. The text reads naturally in one go!

🧮 What Else Can f-strings Do?

You can run math expressions directly inside the braces!

# For example, let's calculate spell damage:
base_damage = 20
bonus_fire = 15

print(f"The magic ball deals {base_damage + bonus_fire} points of damage!")
# Output: The magic ball deals 35 points of damage!

The key rules to remember:
- Put f at the very start (no space before the quote): f"My text"
- Variables go inside {} (curly braces)
- Reads like natural language and looks like plain text — ideal for a beginning engineer!

Your reaction to the article

💬 Comments (0)

🔐 Sign in to leave a comment
🚪 Login
💭

No comments yet

Be the first to share your opinion about this article!

🔗 Similar

Similar articles

Continue learning with these materials

📝

Setting Up Your Environment: Python, pip, and VS …

Before writing code locally, you need to set up three tools: Python, pip, and VS...

📅 04.06.2026 👁️ 17
📝

The datetime Module: Working with Dates and Times

datetime is Python's standard module for working with dates and times. It's part of the...

📅 08.05.2026 👁️ 67
📝

.env Files and Environment Variables: Keeping Sec…

Imagine you wrote a program with an API key hardcoded in the source and pushed...

📅 08.05.2026 👁️ 76

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!