Stop concatenating strings with +. It's clumsy and you'll get a TypeError
the moment you forget str()
. There are better ways. Here are the main ones, from best to worst.
1. F-Strings (Formatted String Literals)
This is the standard. Use this. It was introduced in Python 3.6.
F-strings are fast, readable, and concise. You put an f
before the string and put your variables directly inside curly braces {}
.
How to use it:
name = "John"
age = 30
print(f"User is {name}, age is {age}.")
You can also do expressions inside the braces:
print(f"In 5 years, user will be {age + 5}.")
There's a useful trick for debugging. If you want to see the variable name and its value, just add an equals sign:
print(f"{name=}, {age=}")
# Output: name='John', age=30
This is fast for checking values. No more typing the variable name twice.
You can also format numbers and dates directly inside.
price = 59.955
print(f"Price: ${price:.2f}") # Formats to 2 decimal places
# Output: Price: $59.96
Bottom line: F-strings are the preferred method. They are the cleanest and most readable way to format strings.
2. The .format() Method
Before f-strings, this was the best way. It's more powerful than the old % style but more verbose than f-strings. You will see this in code written before Python 3.6 or in situations where you need to separate the template string from the data.
How to use it:
name = "Jane"
age = 28
print("User is {}, age is {}.".format(name, age))
The braces {}
are placeholders. The variables from .format()
fill them in order.
You can also use numbered or named placeholders, which helps readability if you have many variables.
# Numbered
print("User is {0}, age is {1}.".format(name, age))
# Named
print("User is {user_name}, age is {user_age}.".format(user_name=name, user_age=age))
Using named placeholders is better when the string is complex.
When to use it: If you need to support Python versions older than 3.6, or if you have a formatting template stored in a config file that you apply later. Otherwise, just use f-strings.
3. The % Operator (Old Style)
This is the original C-style formatting. It's now considered old and is less flexible. You will see it in a lot of old tutorials, legacy code, and in some specific standard library modules like logging
.
Don't write new code with this method unless you have a very specific reason.
How it looks:
name = "Peter"
age = 45
print("User is %s, age is %d." % (name, age))
%s
is for strings, %d
is for integers, %f
is for floats. You have to match the types, which is annoying. It gets hard to read when you have multiple variables.
Why you shouldn't use it: It's less readable and more error-prone than f-strings or .format()
. The official documentation discourages its use for new code.
Conclusion
- Use f-strings. They are the modern standard for a reason.
- Use
.format()
if you have to deal with old Python versions or complex templates. - Recognize the
%
style so you can understand old code, but don't use it for new projects.
Stick to f-strings and your code will be cleaner. That's it.
We will learn in the next lesson:
- 👉 Comments in Python