Is the following behavior in the Python documentation (Python 2.7)?
>>> '{:20}'.format(1e10)
' 10000000000.0'
>>> '{:20g}'.format(1e10)
' 1e+10'
In fact, the first result surprises me: the documentation indicates that not specifying the format type ('f', 'e', etc ..) for float is equivalent to using the general format "g". This example shows that this does not seem to be the case, so I got confused.
Perhaps this is due to the fact that "The general convention is that an empty format string (" ") gives the same result as you called str () for the value."? Actually:
>>> str(1e10)
'10000000000.0'
However, in the case of format, the format {:20}string is not empty (this 20), so I got confused.
So, is this behavior {:20}defined exactly in the documentation? Is the exact behavior str()on the floats exactly defined ( str(1e11)has an indicator, but not str(1e10)...)?
PS: My goal is to format the numbers with uncertainty so that the result is very close to what the float gave (presence or absence of an exponent, etc.). However, it’s hard for me to find the exact formatting rules.
PPS: '{:20}'.format(1e10)gives a result other than formatting the line '{!s:20}'.format(1e10), where the line is flushed to the left (as usual for the line), and not to the right.
source
share