Printing and f-strings

We’ve encountered the print function a few times in this Session and its easy to use - you provide some variables or values and it prints them to the screen. However, we can use f-strings to format the appearance of the information we’re printing so that it looks a little bit neater than the default.

To see how and why, lets return to our first exercise on the Haber-Bosch process. Written as a single block, our code was

p_N2 = 1.00
p_H2 = 3.00
p_NH3 = 3.82e-2

K = p_NH3 ** 2 / (p_N2 * p_H2 ** 3)

delta_G = -scipy.constants.R * temperature * math.log(K)

delta_G /= 1000

print(delta_G)
24.345175281081325

The code works and we get the right answer, but we’ve got far too many significant figures, and we haven’t written any units - our presentation is bad.

To tidy this up, we can use an f-string when we print. An f-string lets you lay out how numbers, strings, and even booleans will be printed to the screen.

Here’s a basic example

score = 10
print(f'I give CH12004 a {score}/10!')
I give CH12004 a 10/10

The f-string is defined by the use of f just before the first quotation mark, and the subsequent use of braces (curly brackets) into which we can put the name of the variable we want to print.

Let’s now apply this to the Haber-Bosch exercise

print(f'delta_G = {delta_G} kJ mol^-1')
delta_G = 24.345175281081325 kJ mol^-1

That’s a little bit better, but we’ve still got too many numbers! We can use more advanced features of f-strings to format the number of decimal places - let’s imagine we want the number to two decimal places.

print(f'delta_G = {delta_G:.2f} kJ mol^-1')
delta_G = 24.35 kJ mol^-1

or to three significant figures

print(f'delta_G = {delta_G:.3g} kJ mol^-1')
delta_G = 24.4 kJ mol^-1

That’s much better! Here we’ve used a format specifier to tell Python that delta_G is float that we want to represent as either

  1. A string with two decimal places using the :.2f specifier.
  2. A string with three significant figures using the :.3g specifier.

In both cases Python has done the “rounding” for us when printing the variable, but has not “rounded” the variable itself.

Warning

Always use f-strings and format specifiers when you want to show data to a certain number of decimal places (or significant figures). Never use a rounding function - these are prone to errors and is unneccessary.

Some common format specifiers for str, float, and int variables.
Syntax Description
{variable} Unformatted print - use only with str variables
{variable:nf} Print a float with n decimal places
{variable:ng} Print a float with n significant figures
{variable:nd} Print an int which contains n digits
Tip

There are different format specifiers for different types, and lots of ways of manipulating information using identifiers - remembering the above table will get you most of the way there, but you can find out more about the different format specifiers and identifiers in the Python documentation.

There’s also a much easier to understand version here which contains most of what you’ll need to know.

We can go one step further and use some unicode characters to make this the perfect print statement.

print(f'ΔG = {delta_G:.3g} kJ mol⁻¹')
ΔG = 24.4 kJ mol⁻¹