Using F-Strings to Format and Round Floats
00:00 Let’s start exploring floats and f-strings. In a terminal, you can launch Python and you can start with a standard string. And let’s use the number one third so we can say one third is 1/3.
00:14
Now this is a standard string, so all the characters within it are represented literally, including the one slash three. If you want that to be represented as a float, you can change this to an f-string by adding an f
before the quotation marks, but, you also need to take the expression you want to evaluate, in this case, one divided by three, and then enclose it with curly brackets.
00:38 The f-string will first evaluate one divided by three, which gives us a float, and the f-string will display it as a float 0.33333 recurring. Now in some applications, maybe even in many applications, you may not want to display all of those digits after the decimal point.
00:56 So if you want to, for example, only display two digits after the decimal point, you can use what’s called the format specifier. And the way to introduce a format specifier in your f-string is to add a colon within the curly brackets right after the expression you want to evaluate, which in this case is one divided by three. And f-strings have a mini-language to deal with these and we’re going to be exploring some parts of this mini-language in this course.
01:25
If you would like to display this number with two digits after the decimal point, you can put dot, to represent the decimal point, and then 2
to represent that you want two digits after the decimal point, and then an f
to indicate this is a float.
01:41
So the colon :.2f
, which may look bizarre, but you get used to the format of these format specifiers, tells Python, I would like you to display this float using two digits after the decimal point.
01:55 And that’s where we have ‘One third is 0.33’. We no longer have all the remaining threes. This only affects how the number is displayed. It doesn’t affect the number itself.
02:07 For example, if the number were stored in a variable, that value would be unaffected. The only thing that changes is how the string displays it as an output.
02:17
Let’s look at another example. Let’s assume you want to define a function called total_price()
, which will take in a cost and a tax rate. And then yes, there’s always tax on things unfortunately, you might want to return the cost
02:33 multiplied by one plus the tax. So if the tax is, let’s say, 20%, you’d have 0.2, and therefore this would be cost multiplied by 1.2. So that’s a simple function. Let’s give it some value.
02:45
So let’s say we have cost_price
is equal to a thousand dollars and the tax is equal to 20%. So 0.2, let’s show the full calculation using an f-string.
02:56
So, dollar sign and let’s put the cost_price
. And for the time being, you don’t need to use any format specifiers, we’ll add these in a bit. So that’s a cost_price
.
03:06
You can add to it, the cost_price
multiplied by the tax, and that will be equal to,
03:15
we can now call the function total_price()
using cost_
price
and tax
. And we need to close the quotation marks.
03:25 And that gives us a thousand dollars plus 200 is equal to $1,200,
03:32 but you can see that the numbers are not consistent. The thousand, which was an integer, is displayed as an integer. However, once you’ve multiplied by tax, it’s showing it even though 200 is itself a whole number, Python displays it as a float, as is the 1,200.
03:47
You want this to be consistent, as we’ve seen earlier with prices. You normally want to either show the .00
to show the cents or not show them at all.
03:57 Let’s say we want to show them all, and we also want to put in the comma to show where the thousands are. Let’s start one at a time. So to indicate to our f-string that we want these floats to be displayed using two decimal points or two digits after a decimal point, you can put the colon, which separates the expression you want to evaluate in the curly brackets from the format specifier.
04:21
And the format specifier is .2f
. So two digits after a decimal point and this is a float, and that applies to the 1000, but you want to apply it to the others.
04:33
In this case, the expression is cost_
price
multiplied by tax
. So this is where the colon goes. And finally, same thing with our final one.
04:43 And there we have the expression showing us that all the values now have a decimal point and two digits after decimal points. They’re all zeros in this case, but they don’t have to be.
04:53 You can have other values which have cents, and then the tax might also have its own cents.
04:59 And let’s add one more thing to the format specifier, and we can add it to all of them right after the colon and before the dot. You can put a comma and you can do it on all of them because we want the same to apply.
05:12 And this is telling Python’s f-strings that you want to use the thousand separator and any number which is larger than a thousand, you can see that the comma has been inserted.
05:23 200 is not affected, but the 1000 and the 1,200 now have the comma as a thousand separator.
05:33 Let’s finish this lesson by summarizing the key points. You can add format specifiers after the expression you put within the braces in an f-string. The expression evaluates to some data and that’s what you want to display.
05:47 And after the expression, you can put the format specifiers.
05:51 You use a colon to separate this expression from the format specifier you want to use to decide how you want to format your data.
06:01 Python’s f-strings have a mini-language to specify your formatting requirements. However, this is not the sort of thing you need to memorize. If you use them often enough, you start to remember many of them, or if not, you know where to find them, such as in the documentation.
06:17
And in this lesson, we’ve had a look at a couple of format specifiers. In one of them, you can use a dot followed by an integer and the letter f
, and this shows that the expression should be treated as a float and displayed using two digits after the decimal point.
06:31
So this example, which is similar to one we used in the lesson, the expression is one divided by three, which evaluates to a float. Then you have a colon followed by .2f
.
06:42 So this number will be displayed with two digits after the decimal point.
06:49 And you can also use a comma after the colon, and this will add the thousand separator for numbers that need it. Here are two examples. You can have the number a thousand with just a comma after the colon.
07:01
So this will display 1000 with a comma after the one. Or you can put ,.2f
after the colon, and this will show 1000 with the thousand separator, the comma between the one and the first zero, but also two digits after the decimal point, which in this case would be .00
.
07:20 But there’s lots more formatting you can do with floats and f-strings. We’ll have a look at these in the following lessons.
Become a Member to join the conversation.