How to Format Decimal Places in Python Using f-Strings?

As a developer working on numerous projects across the USA, I recently faced an issue where I needed to format currency values with a specific number of decimal places for our financial reports. After researching various string formatting techniques, I found that using f-strings is an efficient and readable way to format decimal places. I will help you learn how to format decimal places in Python using f-strings in this tutorial with examples.

f-Strings in Python

F-strings, introduced in Python 3.6, provide a concise and expressive way to embed expressions inside string literals for formatting. They allow you to include variables, expressions, and even function calls directly within the string.

Read How to Extract Numbers from a String in Python?

Format Decimal Places with f-Strings in Python

To format a decimal number with a specific number of decimal places using f-strings in Python, you can use the .nf syntax, where n represents the desired number of decimal places. Here’s an example:

revenue = 1234.5678
print(f"The company's revenue is ${revenue:.2f}")

Output:

The company's revenue is $1234.57

I have executed the above example code and added the screenshot below.

Format Decimal Places in Python Using f-Strings

This example {revenue:.2f} specifies that the revenue variable should be formatted as a floating-point number with two decimal places.

Read How to Split a Long String into Multiple Lines in Python?

Example: Format Currency Values

Let’s consider a real-world scenario where we need to format currency values for a financial report. Suppose we have a list of transactions for a US-based company:

transactions = [
    {"customer": "John Smith", "amount": 124.99},
    {"customer": "Emma Johnson", "amount": 50.75},
    {"customer": "Michael Brown", "amount": 89.50},
    {"customer": "Olivia Davis", "amount": 200.00}
]

To format the transaction amounts with two decimal places and a dollar sign, we can use f-strings as follows:

for transaction in transactions:
    customer = transaction["customer"]
    amount = transaction["amount"]
    print(f"{customer}: ${amount:.2f}")

Output:

John Smith: $124.99
Emma Johnson: $50.75
Michael Brown: $89.50
Olivia Davis: $200.00

I have executed the above example code and added the screenshot below.

How to Format Decimal Places in Python Using f-Strings

By using {amount:.2f}, we ensure that each transaction amount is displayed with exactly two decimal places, making the output consistent and readable.

Check out How to Pad a String with Zeros in Python?

Advanced Formatting with f-Strings

F-strings offer additional formatting options that can be useful when working with decimal places:

1. Thousands Separator

To format large numbers with a thousand separator, you can use the , symbol within the f-string. For example:

revenue = 1234567.89
print(f"The company's annual revenue is ${revenue:,.2f}")

Output:

The company's annual revenue is $1,234,567.89

I have executed the above example code and added the screenshot below.

Format Decimal Places in Python Using f-Strings advance

Read How to Find the Last Occurrence of a Substring in a String Using Python?

2. Percentage Formatting

If you need to format decimal numbers as percentages, you can use the % symbol. Here’s an example:

growth_rate = 0.0875
print(f"The company's growth rate is {growth_rate:.2%}")

Output:

The company's growth rate is 8.75%

Check out How to Iterate Through a String in Python?

Conclusion

In this tutorial, I have explained how to format decimal places in Python using f-strings. I discussed f-strings in Python, how to format decimal places with f-strings , and a real-world example. I also discussed some advanced formatting with f-strings.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.