Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

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.

00:09 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.

00:21 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.

00:29 However, most programmers today use Python’s formatted string literals, or f-strings, which look simpler and offer even more functionality. You can create a formatted string by prefixing your normal string literal with either a lowercase or uppercase letter F.

00:45 For the most part, it’ll work like a regular string, so you can type your message there. 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.

01:36 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.

02:51 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.

Avatar image for Gokul Thiruvengadam Rajagopalan

Gokul Thiruvengadam Rajagopalan on Dec. 1, 2023

f'{92.343:.1%}'

Ouput: 9234.3 (Why?)

Not able to understand. Please clarify.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Dec. 1, 2023

@Gokul Thiruvengadam Rajagopalan Here’s an excerpt from the documentation mentioned in the video:

Percentage. Multiplies the number by 100 and displays in fixed (‘f’) format, followed by a percent sign. (Source)

So, when you use the percent sign as your formatting, it’ll multiply the number by one hundred:

>>> f"{92.343:%}"
'9234.300000%'

Additionally, when you combine the percent sign with .1, it’ll round the number to one decimal point, disregarding the trailing zeros in the output string:

>>> f"{92.343:.1%}"
'9234.3%'

Become a Member to join the conversation.