In Post #17, we learned how to build strings by concatenating them with the +
operator. While it works, you probably noticed it can get a bit messy, with lots of quotes, plus signs, and calls to str()
. It’s easy to forget a space or a +
and get an error.
Thankfully, modern Python (version 3.6 and newer) introduced a far superior way to format strings: f-strings. They are cleaner, more readable, and more powerful.
This post will introduce you to f-strings and show you why they should be your go-to method for embedding variables and expressions into text.
The Old Way: A Quick Recap
To appreciate the elegance of f-strings, let’s quickly revisit the concatenation method for building a message with variables.
player_name = "Alex"
score = 150
# The concatenation method
message = "Player " + player_name + " has a score of " + str(score) + "."
print(message)
Notice the four +
signs and the necessary str(score)
conversion. This code works, but it’s clunky.
The Modern Way: f-String Syntax
An f-string (short for “formatted string literal”) lets you embed Python expressions directly inside a string literal, making the process incredibly intuitive.
You create one by prefixing the opening quotation mark with the letter f
(either lowercase or uppercase). Then, you can place your variables directly inside curly braces {}
within the string.
Let’s rewrite our previous example using this modern syntax.
player_name = "Alex"
score = 150
# The f-string method
message = f"Player {player_name} has a score of {score}."
print(message)
The output is identical: Player Alex has a score of 150.
Compare the two methods. The f-string is a clear winner for several reasons:
- Readability: The f-string looks almost exactly like the final output. The structure is much easier to see at a glance.
- Convenience: There are no more
+
signs needed to join the different parts of the string. - Automatic Conversion: Notice we didn’t have to use
str(score)
. F-strings handle the conversion of numbers (and other data types) to strings for you automatically.
The Power of Expressions
The real magic of f-strings is that you can put any valid Python expression inside the curly braces, and it will be evaluated when the string is created. This means you can do calculations directly inside the string.
Let’s look at an example that combines what we learned about operators in Post #12.
number_a = 10
number_b = 5
print(f"The sum of {number_a} and {number_b} is {number_a + number_b}.")
When this code runs, Python first calculates number_a + number_b
(which is 15
), and then places that result into the string. The output is:
The sum of 10 and 5 is 15.
This ability to evaluate expressions on the fly makes f-strings an incredibly powerful and flexible tool.
What’s Next?
You’ve now learned about f-strings, the modern, readable, and efficient way to format strings in Python. By using the f
prefix and curly braces {}
, you can create clean, dynamic text with minimal effort. From now on, you should prefer f-strings over manual concatenation for building your strings.
We’ve used the assignment operator (=
) to store values in variables since Post #8. But Python provides several shorthand assignment operators that can make our code more concise when we’re modifying a variable’s existing value. In Post #19, we will explore these handy assignment operators.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com