Master Python

First steps

6 / 253
Contents

Contents

Master Python

0 of 253 complete

8 min read

Putting a value inside a sentence

Writing a sentence with a hole in it, and letting Python fill the hole in.

Most of the output you will ever write is a sentence with a value sitting inside it. "Your total is 750 pence." The sentence is fixed, the number changes, and you want the two joined neatly, with no stray spaces and no fuss.

You have already seen one way to do it: hand print several things separated by commas and let it put a space between them. It works, and for a quick look at a value it is often all you need.

Try CodePython

Change 250 to 195 and run it again. Then try removing the full stop from the last piece of text.

It gets awkward as soon as you want anything other than a single space between the parts, or when the sentence is long enough that the commas start hiding the shape of it. The tidier way is to write the sentence out as one piece of text, leaving a hole where the value belongs.

Put the letter f immediately before the opening quotation mark. Then, anywhere inside the text, open a curly bracket, put something to be worked out, and close the bracket. Whatever is in the brackets is worked out first and dropped into the hole. A piece of text written this way is called an f-string, and the f stands for format.

print(f"Your total is {3 * 250} pence.")
The f goes outside the quotation marks. The curly brackets go inside.
Try CodePython

Change 3 to 12 and run it again. Then move the closing curly bracket to just after the 3 and look at what comes out.

The f is the whole difference between a sentence with a hole in it and a sentence with two curly brackets in it. Left off, the curly brackets are simply more characters of text.

Try CodePython

The only difference is the f at the start of the second line. Delete it and run again, so that both lines behave the same way, then put it back.

You can have as many holes as you like, with as much or as little text between them.

Try CodePython

Change every 3 to a 4 and run it again. Then change only the first one and notice that nothing stops you printing a sentence that is untrue.

One extra piece of punctuation is worth having now, because division produces long numbers and long numbers look untidy in a sentence. Inside the curly brackets you can add a colon and a small instruction about how to display the value.

The instruction is written .2f, and it means: show this as a decimal number with two digits after the point. The f inside the curly brackets is a different f from the one at the front of the string. This one is short for float, the name you met for a number with a decimal point, and it says which shape to display the value in.

Try CodePython

Change the 2 to a 4 and run it again, then to 0. Then delete the :.2f altogether and compare the two lines of output.

What does print(f"{2} and {2} make {2 + 2}") write out?

2 and 2 make 4. Each pair of curly brackets is worked out on its own and replaced by its result, and the ordinary text between them is carried through untouched.

All lessons in Master Python