Implement the same exercise as Exercise 1 (Create a program that asks the user to enter their name and their age. Print out a message addressed to them that tells them the year that they will turn 100 years old), except use f-strings instead of the +
operator to print the resulting output message.
One very common operation programmers need to do is display information in the form of text output. In Python we use the print()
function for this, but we need to keep one thing in mind: the print()
function only takes strings. That means, when we want to display an integer, we need to convert it to a string before passing it to the print()
function. When we have complicated numbers to display, this becomes burdensome.
Instead, there is a built-in Python functionality called f-strings, short for formatted string literals that solve the problem for us!
For me, the most common use of f-strings is to automatically display variables in a string. The mechanism to do this is:
"
from the string{ }
inside the string.For example, the following code displays my favorite color in a sentence:
No need for concatenating strings with the +
sign! Similarly for numbers:
There are all kinds of options for formatting decimals too - how many numbers after the decimal point, pad with zeroes, etc. that the official documentation goes into in great detail, with examples, so I won’t do that here. Note that the examples are shown with the “old” .format()
way of string formatting in Python, but it applies to f-strings as well.
The really truly versatile thing that f-strings allow you to do is display expressions and not just variables. In some cases this is very useful, mostly for readability:
Hopefully with f-strings, you have a more compact way of displaying information to the terminal!