Last updated: Apr 8, 2024
Reading timeยท3 min
Use a formatted string literal to add zeros to a float after the decimal.
Formatted string literals can use the format specification mini-language to add N zeros to a floating-point number after the decimal.
my_float = 3.0 result = f'{my_float:.3f}' print(result) # ๐๏ธ '3.000'
Formatted string literals
(f-strings) let us include expressions inside of a string by prefixing the
string with f
.
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # ๐๏ธ is subscribed: True
Make sure to wrap expressions in curly braces - {expression}
.
We are also able to use the format specification mini-language in expressions in f-strings.
my_float = 3.2 result_1 = f'{my_float:.3f}' print(result_1) # ๐๏ธ '3.200' result_2 = f'{my_float:.5f}' print(result_2) # ๐๏ธ '3.20000' result_3 = f'{my_float:.6f}' print(result_3) # ๐๏ธ '3.200000'
The digit after the period is used to determine how many decimal places the float should have.
You can also explicitly specify the fill character (0
) after the period.
my_float = 3.2 result_1 = f'{my_float:.03f}' print(result_1) # ๐๏ธ '3.200' result_2 = f'{my_float:.05f}' print(result_2) # ๐๏ธ '3.20000' result_3 = f'{my_float:.06f}' print(result_3) # ๐๏ธ '3.200000'
When no explicit alignment is given preceding the width field by a zero enables us to zero-pad the number after the decimal.
You can also use this approach if the width of the float after the decimal is stored in a variable.
my_float = 3.2 width_after_decimal = 4 result = f'{my_float:.{width_after_decimal}f}' print(result) # ๐๏ธ '3.2000'
Notice that we used a second set of curly braces in the expression to evaluate the variable.
The formatted string literal approach will always return a value that is a string.
There is no way to pad the float with zeros after the decimal and have it remain a number because Python doesn't keep insignificant trailing zeros around.
my_float = 3.00000000 print(my_float) # ๐๏ธ 3.0
You can also use the str.format()
method to add zeros to a float after the
decimal.
my_float = 3.0 result = '{:.3f}'.format(my_float) print(result) # '3.000' result = '{:.4f}'.format(my_float) print(result) # '3.0000' result = '{:.6f}'.format(my_float) print(result) # '3.000000'
The str.format() method performs string formatting operations.
first = 'bobby' last = 'hadz' result = "Name: {} {}".format(first, last) print(result) # ๐๏ธ "Name: bobby hadz"
The string the method is called on can contain replacement fields specified
using curly braces {}
.
We used the format specification mini-language, just like we did when we used a formatted string literal.
Alternatively, you can use the format()
function.
my_float = 3.0 result = format(my_float, '.3f') print(result) # ๐๏ธ '3.000' print(type(result)) # ๐๏ธ <class 'str'>
We used the format() function to add zeros to a float after the decimal.
The f
type in the format specifier stands for fixed-point notation.
my_float = 3.0 result_1 = format(my_float, '.3f') print(result_1) # '3.000' result_2 = format(my_float, '.4f') print(result_2) # '3.0000' result_3 = format(my_float, '.6f') print(result_3) # '3.000000'
Notice that the value the format()
function returns is a string.
This is necessary because Python doesn't keep any non-significant, trailing zeros around.
my_float = 3.00000000 print(my_float) # ๐๏ธ 3.0
This approach can also be used when the width of the float after the decimal is stored in a variable.
my_float = 3.0 width_after_decimal = 3 result_1 = format(my_float, f'.{width_after_decimal}f') print(result_1) # '3.000'
You can learn more about the related topics by checking out the following tutorials: