Format a floating-point number to fixed width in Python

avatar

Borislav Hadzhiev

Last updated: Aug 29, 2022

banner

Photo from Unsplash

Format a floating-point number to fixed width in Python #

Use a formatted string literal to format a floating-point number to a fixed width, e.g. result = f'{my_float:8.3f}'. The formatted string literal will format the number to the specified width with the specified number of digits after the decimal.

main.py
my_float = 4.56 # ✅ format float to fixed width of 8 with 3 digits after the decimal result = f'{my_float:8.3f}' print(repr(result)) # 👉️ ' 4.560' # -------------------------------------- my_list = [1.23, 2.34, 4.56, 5.67] # ✅ format a list of floating-point numbers to fixed width # 👇️ fixed with of 5 with 1 digit after decimal result = [f'{item:5.1f}' for item in my_list] print(result) # 👉️ [' 1.2', ' 2.3', ' 4.6', ' 5.7']

We used a formatted string literal to format a floating-point number to a fixed width.

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

The digit right after the colon is the width to format the number to.

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

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

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}.

Formatted string literals also enable us to use the format specification mini-language in expression blocks.

main.py
# 👇️ format float to a fixed width of 6 with 1 decimal print(repr(f'{1.23:6.1f}')) # ' 1.2'

Format a list of floating-point numbers to fixed width #

To format a list of floating-point numbers to a fixed width:

  1. Use a list comprehension to iterate over the list.
  2. Use a formatted string literal to format each float to a fixed width.
  3. The new list will contain strings storing the float values, formatted to the specified width.
main.py
my_list = [1.23, 2.34, 4.56, 5.67] # 👇️ format each float to fixed length of 5 with 1 decimal place result = [f'{item:5.1f}' for item in my_list] print(result) # 👉️ [' 1.2', ' 2.3', ' 4.6', ' 5.7']

We used a list comprehension to iterate over the list of floating-point numbers.

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 use a formatted string literal to format the float to a fixed length of 5 with 1 decimal place.

Alternatively, you can use a for loop.

To format a list of floating-point numbers to a fixed width:

  1. Declare a new variable and initialize it to an empty list.
  2. Use a for loop to iterate over the list of floating-point numbers.
  3. Format each float to a fixed width and append the result to the new list.
main.py
my_list = [1.23, 2.34, 4.56, 5.67] new_list = [] for item in my_list: new_list.append(f'{item:5.1f}') # 👇️ format each float in the list to fixed width of 5 with 1 decimal print(new_list) # ️ 👉️ [' 1.2', ' 2.3', ' 4.6', ' 5.7']

We used a for loop to iterate over the list of floating-point numbers.

On each iteration, we format the current number to a fixed length of 5 with 1 decimal place and append the result to the new list.

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.