Formatting Floats Inside Python F-Strings (Overview)
You’ll often need to format and round a Python float
to display the results of your calculations neatly within strings. In earlier versions of Python, this was a messy thing to do because you needed to round your numbers first and then use either string concatenation or the old string formatting technique to do this for you.
Since Python 3.6, the literal string interpolation, more commonly known as a formatted string literal or f-string, allows you to customize the content of your strings in a more readable way.
An f-string is a literal string prefixed with a lowercase or uppercase letter f
and contains zero or more replacement fields enclosed within a pair of curly braces {...}
. Each field contains an expression that produces a value. You can calculate the field’s content, but you can also use function calls or even variables.
00:00 Python introduced f-strings in version 3.6 of the language. And since then, they have very quickly become most programmers’ favorite way of formatting strings.
00:10 And a very common data type to use in f-strings is floats because they appear everywhere in our programs. In this course, we’ll explore the various options that f-strings have to format floats the way you want them in your program.
00:25 Let me give an example. Let’s assume you’re dealing with a circle and you want to display the value of the mathematical constant pi.
00:32 Possibly you may not want to display all of those digits after the decimal point. Maybe you do in some applications, but in many other applications, if you want your program to output the value, it may be sufficient to show fewer digits.
00:46 For example, 3.1416, or maybe just 3.14. You can do this with f-strings, and this is what we’re going to be looking at in the next lesson in this course.
00:58
Here’s another example. There’s a dictionary called products
with key-value pairs, and the keys are the names of products and the values are their prices.
01:07 And you decide you want to display the price list for this food outlet in a table. The output shows you the products and their prices, but it’s not displayed in the best possible way.
01:18
You may want the prices to be aligned with each other. And if you look at “Cookies”, which is $3, normally we want to show the .00
, even when it’s a whole number of dollars.
01:29 You can modify your code using f-strings to display the table in a neater and clearer fashion. Here you can see that all the prices are aligned, and even “Cookies” are showing as $3.00 to make it nice and neat.
01:43 And this ensures that the customers of this food outlet will not get confused by the price list.
01:50 And here’s a final example to show you some of the things you can do when formatting floats. Here’s a number 1,234.56. And often you may want to show the thousand separator by putting in a comma between the one and the two.
02:04 So 1,234.56. This is how we often show numbers in everyday life. However, different countries around the world have different conventions on how to display such numbers.
02:17 For example, in some places you may replace the dot that represents the decimal point with a comma. And in other parts of the world, the thousand separator is itself the dot.
02:28 So the dot and the comma reverse, you have the dot as the thousand separator and the comma instead of the decimal point. These are all customizations that you can do directly using f-strings.
02:40 So let’s see what’s coming up in the course. In the coming lessons, you’ll learn how to format and round floats, how to customize the width of a formatted string, such as to create the price list in a nice table as the example you’ve seen earlier.
02:55 You’ll also look at how to round complex numbers or numbers when using scientific notation. And finally, you’ll look at how to format numbers for international use to adapt to the different conventions used around the world.
Become a Member to join the conversation.