Using %-formatting, I can round the number of decimal cases in a string:
pi = 3.14159265
print('pi = %0.2f' %pi)
And in output(in terminal) this would give me:
pi = 3.14
Can I use f-strings do this task? This feature has been added in Python 3.6
Include the type specifier in your format expression
format specifier:
f'{value:{width}.{precision}}'
example:
# Formatted string literals
x = 3.14159265
print(f'pi = {x:.2f}')
Yes. See the Format Specification Mini-language:
>>> pi = 3.14159265
>>> print(f'{pi:.2f}')
3.14
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With