Formatting Floats Inside Python F-Strings (Summary)
In this video course, you covered a lot of ground that was hopefully interesting and a little challenging. You now know how to use Python’s format specification mini-language to round and display floats and Decimal
objects in standard ways.
You also know how to deal with both scientific and complex numbers. You touched on some of the older ways to round numbers within strings, and saw how revolutionary the f-string syntax is. You even learned how to format numbers inside strings internationally.
Now that you’ve had a thorough workout formatting numbers, you should experiment further and consider delving deeper into f-strings and the format specification mini-language that supports them. There’s still lots more for you to learn.
Congratulations on completing this video course, and may all your strings be neat.
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 Well done. You’ve made it to the end of this course. If you didn’t already love Python’s f-strings, hopefully you do now, like many Python programmers do.
00:09
Let’s have a look at some of the course highlights. You learned how to format and round floats. For example, in this case, the format specifier after the colon, there’s a comma to show you want the thousand separator and the .2f
to show that you want two digits after the decimal point. You then learn how to customize the width of formatted strings.
00:30 In this example, which is similar to the previous one, but also has the less than symbol, which indicates you would like to align left and that 12 immediately after it, which is the overall width of the display string.
00:44
And you learned how to deal with scientific notation using the e
format specifier. Here the speed of light is displayed showing five digits after a decimal point and then Python puts in the e
notation.
00:58
And finally, you learned how to deal with numbers that can be displayed differently in different parts of the world. You need help from the locale
module in the standard library, and then you can use the n
format specifier to format your numbers depending on your current locale.
01:15 And you can also look at things such as currency and put in the correct currency symbol and format.
01:22 But there’s lots more to learn when using f-strings to format floats. There are more format specifiers and more ways you can use them. You can look at the accompanying tutorial, “How to Format Floats Within F-Strings in Python”, as well as some other Real Python resources, which dive into more detail about how f-strings work.
01:42 So now it’s your turn. You can open a REPL or your favorite Python editor and you can start exploring how to format floats using Python’s f-strings. Good luck.
Become a Member to join the conversation.