9.10. F-Strings

The string .format() method allows us to format a string in a way that is similar to fill-in-the-blank. The return value of .format() is a string that can then be used and displayed using the print() function.

Another (more modern) approach is to use an f-string. F-strings are a Python-specific way of formatting a string (using a fill-in-the-blank mechanism like .format()) without using a separate method call.

Let’s revisit the example we used before. Pay attention to how the same outcome can be obtained first with the .format() method and then with the f-string approach.

In the above example, using the f-strings approach, we literally fill each placeholder (i.e., each pair of braces) with a variable whose value we want to display.

Note that to use an f-string, we must type the character “f” before the string content. We can then enter expressions within the string between curly braces ({}). We can use almost any values or expression inside the braces. It can be: a value; a variable that contains or references a value; an arithmetic expression; a string expression; a method call that returns a value such as a string or a number. See the following examples that show how we can use values, variables, and different expressions inside the braces. Each print() statement produces the exact same output.

We can use values directly inside the braces.

We can use expressions (i.e., string operations and arithmetic operations) directly inside the braces.

We can use expressions consisting of variables directly inside the braces.

We can call a method (also a type of expression) directly inside the braces. As an extreme example, we can even use the format method, which returns a string, inside f-strings. Note that in this example, we use max(), a built-in method that will return the highest value among the values we provide. Since the value 96.75 is assigned to the variable score and is greater than 60, the value returned from max(score, 60) will be 96.75.

Similar to the format() approach, we can use format specifiers (e.g., :.2f) to further fine-tune the value being displayed. For instance, if we want to display a floating-point number with one decimal place, we can use :.1f inside the braces and after the expression. The example below shows how we can apply the format specifiers with both a variable and a method call inside the braces.

At this point, we might ask, is f-strings the best method to use for formatting strings? Well, as usual, it depends. Here we describe two caveats to keep in mind when using f-strings.

First, we need to pay attention to using quotes inside f-strings. If we use quotes, that means we are embedding quotes inside the quotes required by f-strings. If we use the same type of quotes, such as double quotes, the Python interpreter will have trouble determining how these double-quotes are paired with one another, and it will have trouble understanding what we want a computer to do. A solution is to use a different kind of quotes, such as single quotes, so that the Python interpreter knows how to pair those quotes (e.g., double with double, single with single) and properly execute our code. Take a look at the following example, which produces an error, and see if we can fix the bug to have the correct output similar to the previous example (hint: replacing a pair of double quotes).

Note that, as the format() approach does not require using expressions directly inside the format string, we don’t have to worry about the quotes-inside-quotes problem when using the format() approach. The following example uses double quotes throughout.

Second, similar to the format() approach, we need to pay attention when using braces inside f-string, as f-strings already require the use of braces as placeholders. To display a pair of braces inside f-strings, we need to double the pair of braces. If we need double braces in the output, we need to double the pair of double braces, namely four pairs.

Again, since the format() approach does not require using expressions directly inside the format string, we can avoid the braces-inside-braces problem as we described above. As we can see in the following example, since we provide two strings as arguments to format(), we can include the braces as part of the strings for them to be displayed in the output.

In summary, different string formatting methods have their own advantages and disadvantages in terms of readability and caveats. There are other considerations (e.g., speed), but we won’t discuss them here. One of the potential solutions to mitigate the issues raised above is to pre-calculate the values using different expressions and store them in variables. We can then use mostly these variables with format() or f-strings, without using complex expressions directly. See the example inside the question below.

We have introduced various string methods in Python. Use the following question to check if you understand what has been discussed.

You have attempted of activities on this page