f-Strings

Beginning with Version 3.6, Python has come equipped with a powerful tool for creating and formatting strings known as f-strings. We can define an f-string by simply placing an f character in front of the string, immediately before the initial quote.

boring_fstring = f'This is an example of an f-string.'
print(boring_fstring)
This is an example of an f-string.

The example above illustrates the basic syntax for creating an f-string, but it is not particularly useful. We could have obtained the same result using an ordinary string. The benefits of f-strings arise from situation in which we would like to define a string that incorporates values stored in variables. If we place a set of brackets containing a variable name within an f-string, then the value of that variable will be inserted into the string at that location. This is illustrated in the next example.

first = 'Robbie'
last = 'Beane'

name_message = f'Hello. My first name is {first} and my last name is {last}.'
print(name_message)
Hello. My first name is Robbie and my last name is Beane.

In the example above, the values we inserted into the f-string were both strings themselves. However, we can place variables containing numerical values inside of the brackets as well. When doing so, it is NOT necessary to coerce the variable into a string.

We are not restricted to using names of variables inside of brackets in an f-string. We can, in fact, place any expression that we would like inside of the brackets.

These concepts are illustrated in the next example.

z = 3.56
print(f'The square of {z} is {z**2}.')
The square of 3.56 is 12.6736.

Aligning Text with f-Strings

Occasionally, we would like to insert a value into a string with additional spaces padding the value on the left or the right so that the printed value and the additional spaces together constitute a specific number of characters. We can accomplish this using f-strings by following the expression within the braces with a colon :, one of the symbols < or >, and then an integer value. The integer indicates the number of characters that should be set aside for the inserted value, and the selected arrow symbol controls whether the value should be left-justified (<) or right-justified (>).

We illustrated this in the cell below.

my_text = 'text'

print(f'--{my_text:>6}--')
print(f'--{my_text:<6}--')
--  text--
--text  --

This feature of f-strings is particularly useful when we would like to display output in a tabular format, consisting of multiple rows and columns. We can use f-strings to make sure that each entry in a column takes the same amount of space, and is aligned on either the left or the right. This is illustrated below.

num1 = 1
fname1 = 'George'
lname1 = 'Washington'

num2 = 2
fname2 = 'John'
lname2 = 'Adams'

num3 = 16
fname3 = 'Abraham'
lname3 = 'Lincoln'


print('First Name     Last Name      Number')
print('--------------------------------------')
print(f'{fname1:<15}{lname1:<15}{num1:>6}')
print(f'{fname2:<15}{lname2:<15}{num2:>6}')
print(f'{fname3:<15}{lname3:<15}{num3:>6}')
First Name     Last Name      Number
--------------------------------------
George         Washington          1
John           Adams               2
Abraham        Lincoln            16