Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Numbers Formatted as Strings

For more information on f-strings, check out the official documentation.

00:00 In this lesson, you’ll learn how to print numbers in style. Often, you’ll want to combine your numbers with some additional text before presenting it to the user. For example, you may want to insert the price of a product into a prettified placeholder in a templated message. Traditionally, there have been many ways to format numbers in Python. For example, you can call the format() function, which you’ve seen before in this course, to reveal the decimal digits of a floating-point number. However, most programmers today use Python’s formatted string literals, or f-strings, which look simpler and offer even more functionality.

00:38 You can create a formatted string by prefixing your normal string literal with either a lowercase or uppercase letter F. For the most part, it’ll work like a regular string, so you can type your message there.

00:50 For example, "This product costs $3.90". However, unlike a regular string, an f-string lets you additionally specify one or more value placeholders to be filled at a later time.

01:03 Say you wanted to reduce your message across many products with different prices. You can declare a helper variable with a products price, and then replace the concrete value in your f-string with a placeholder by using the curly brackets ({}) and inserting the name of your variable.

01:21 It worked nicely. Notice, however, that the trailing zero in the product’s price got eaten up. Fortunately, those curly braces support a simple formatting language that you can use to alter the appearance of the value in the final formated string. For example, to show the price using exactly two decimal places, you can add an optional format specifier in your placeholder.

01:45 The colon (:) separates an expression to evaluate, such as a variable, from the format specifier. In this case, the specifier is .2f, which rounds the number to two decimal places.

01:58 The letter f tells Python to display the price as a fixed-point number. If a price gets bigger, then you can insert commas to group the integer part of a large number by the thousands.

02:11 Another useful feature of the formatted strings in Python is the ability to format numbers as percentages. For instance, if you have a fractional number like 0.9123, then you can have Python automatically show it as a percentage rounded to the requested number of decimal places.

02:36 The formatting mini-language is powerful and extensive. While you’ve only seen the basics so far, here are a few more examples. You can stop the video now and take a look at them or check the accompanying PDF slides later. For additional information, you’re encouraged to read the official documentation.

02:59 All right, in the next lesson, you’ll revisit complex numbers in Python.

Become a Member to join the conversation.