How to print float values in Python

avatar

Borislav Hadzhiev

Last updated: Sep 2, 2022

banner

Photo from Unsplash

Print float values in Python #

Use the print() function to print a float value, e.g. print(f'{my_float:.2f}'). A formatted string literal can be used if you need to round the float to N decimal places.

main.py
my_float = 456789.4567 # ✅ Print float rounded to 2 decimal places result = f'{my_float:.2f}' print(result) # 👉️ 456789.46 # ✅ Print float rounded to 3 decimal places result = f'{my_float:.3f}' print(result) # 👉️ 456789.457 # -------------------------------------------- # ✅ Print float with comma as thousands separator result = f'{my_float:,.2f}' print(result) # 👉️ 456,789.46

We used the print() function to print integer values.

The print function takes one or more objects and prints them to sys.stdout.

If you have a float value, you can directly pass it to the print() function to print it.

main.py
print(3.14) # 👉️ 3.14 print(6.28) # 👉️ 6.28
Note that the print() function returns None, so don't try to store the result of calling print in a variable.

You can use a formatted string literal to print a floating-point number rounded to N decimal places.

main.py
my_float = 456789.4567 result = f'{my_float:.2f}' print(result) # 👉️ 456789.46 result = f'{my_float:.3f}' print(result) # 👉️ 456789.457

The digit after the period is the number of decimal places the float should have.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
my_str = 'The number is:' my_float = 3.14 result = f'{my_str} {my_float}' print(result) # 👉️ The number is: 3.14

Make sure to wrap expressions in curly braces - {expression}.

If you need to round all floats in a list to N decimal places, use a list comprehension.

main.py
list_of_floats = [123.456, 234.567, 345.678] result = [f'{item:.2f}' for item in list_of_floats] print(result) # 👉️ ['123.46', '234.57', '345.68']

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we round the current float to 2 decimal places and return the result.

You can also use a formatted string literal if you need to print a floating-point number with a comma as the thousands separator.

main.py
my_float = 456789.4567 result = f'{my_float:,.2f}' print(result) # 👉️ 456,789.46 result = f'${my_float:,.2f}' print(result) # 👉️ $456,789.46

You can use this approach to format a floating-point number as currency.

The comma after the colon is the thousands separator and the digit after the period is the number of decimal places the float should have.

Notice that we added a comma after the colon.

F-strings can also be used to format a float to a fixed width.

main.py
my_float = 1.234567 result = f'{my_float:8.3f}' print(repr(result)) # 👉️ ' 1.235'

The example formats the float to a width of 8 with 3 digits after the decimal.

The string gets left-padded with spaces to format the float to the specified length.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.