A very common thing to want to do in Python is to create a string that has the values of variables embedded in it.
The easiest way to do this is to use Python f-strings. The string is prefixed with a lowercase or uppercase ‘F’ and then variables can be placed in the string, surrounded by curly brackets.
day = "Friday"
temperature = 25
text = f"It's {day}; the temperature is {temperature} degrees."
print(text)
It's Friday; the temperature is 25 degrees.
Using C-Style Placeholders
Another way to do the same thing is to use placeholders similar to those found in C and many other programming languages. With this method you need to use placeholders that specify the type of thing you want to embed in the string, and how to format it.
For example:
- %d: embeds a decimal integer
- %f: embeds a floating-point value
- %s: embeds a string
day = "Friday"
temperature = 25
text = "It's %s; the temperature is %d degrees." % (day, temperature)
print(text)
It's Friday; the temperature is 25 degrees.
There are lots of options we can use with these placeholders, as in other programming languages. For example, %.2f specifies that a number should be formatted as a floating-point number with two digits after the decimal point.
day = "Friday"
temperature = 25
text = "It's %s; the temperature is %.2f degrees." % (day, temperature)
print(text)
It's Friday; the temperature is 25.00 degrees.
The String Format Method
In object-oriented programming, functions that are attached to objects are known as methods. String objects in Python have a method called format, and a third option is to use this to embed values in strings. We can use empty {} brackets to specify where the values should go.
day = "Friday"
temperature = 25
text = "It's {}; the temperature is {} degrees.".format(day, temperature)
print(text)
It's Friday; the temperature is 25 degrees.
It’s also possible to use digits starting at zero to specify which argument to the format method goes where.
day = "Friday"
temperature = 25
text = "It's {1} degrees and it's {0}".format(day, temperature)
print(text)
It's 25 degrees and it's Friday
Alternatively we can even name the arguments that we supply to format.
day = "Friday"
temperature = 25
text = "It's {temp} degrees and it's {day_name}".format(day_name=day, temp=temperature)
print(text)
It's 25 degrees and it's Friday
Format String Options
It’s worth trying out all three methods, but most of the time in modern Python you’re probably going to want to use the first option, f-strings. We can supply further options after a colon in the placeholders to determine how the values are formatted.
Notice how similar this is to the C-style format specifiers.
day = "Friday"
temperature = 25
text = f"It's {day} and it's {temperature:.2f} degrees"
print(text)
It's Friday and it's 25.00 degrees
For further information, check the Python docs.
If you’re new to all this, it’s going to seem like there are a lot of confusing options and possibilities. The important thing is really only to try these out and check you can use them. Most of the time you’ll only use a handful of the many possible options that exist.
When you’re new to programming, initially you’ll spend a lot of time having to make sure you’ve got dots and other symbols exactly where you need them. But in time you’ll understand exactly why everything has to be where it is.
Leave a Reply