[Python-3000] PEP 3101 update
Nick Coghlan
ncoghlan at gmail.com
Thu Jun 22 11:49:34 CEST 2006
Greg Ewing wrote:
> Talin wrote:
>
>> s = str.convert( f, "2.2g" )
>
> If format is a string method, then you will already be
> able to do
>
> s = str.format("2.2g", f)
>
> if you want.
Nope. Given the current PEP, it'd have to be one of the following:
s = "{0:2.2g}".format(f)
s = str.format("{0:2.2g}", f)
However, I realised that there's an approach that is aesthetically pleasing
and doesn't require using str() for this - simply consider the leading '{0:'
and trailing '}' to be implicit if there are no braces at all in the supplied
format string.
Then you could do things like:
>>> "b".format(10)
1010
>>> "o".format(10)
12
>>> "x".format(10)
a
>>> "X".format(10)
A
>>> "2.2g".format(10)
10.00
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-3000
mailing list