Can someone help me with the explanation of what the "'f %" meaning in the following code?
print('Slope: %.3f' % lr.w_[1])
Here is a list of string formatting options in Python. You use a %
as a placeholder, which is replaced by lr.w_[1]
in this case. The f
then refers to "Floating point decimal format". The .3
indicates to round to 3 places after the decimal point.
It prints a decimal number, with 3 decimal digit accuracy.
In [1]: print('Slope: %.3f' % 1.123)
Slope: 1.123
In [2]: print('Slope: %.3f' % 1.12345)
Slope: 1.123
In [3]: print('Slope: %.3f' % 1.1)
Slope: 1.100
In [4]: print('Slope: %.3f' % 1.1237)
Slope: 1.124
As you can see by the 4th example is rounds by standard rounding rules
%.3f
f
: Floating point, which means that the value that is going to be printed is a real number.
.3
is for the number of decimals after the point. That means that the real value to be printed will have 3 digits after the point.
For example:
print('%.3f' % 3.14159) # Prints 3.142
print('%.2f' % 3.141592653589) # Prints 3.14
str.format()
instead..format
:print('Slope: {.3f}'.format(lr.w_[1])
print('Slope: {:.3f}.format(lr.w_[1])
. There was a:
missing in @Woohoojin's comment