[Python-3000] PEP 3101 str.format() equivalent of '%#o/x/X'?

wesley chun wescpy at gmail.com
Thu May 29 20:06:58 CEST 2008


> wesley chun wrote:
>>
>> i have to resort to the uglier:
>> >>> 'dec: {0}/oct: 0o{0:o}/hex: 0X{0:X}'.format(i)
>> 'dec: 45/oct: 0o55/hex: 0X2D'


[Nick Coghlan <ncoghlan at gmail.com>]:
> Is being explicit about the displayed prefix really that much uglier? The
> old # alternative display formats were somewhat arbitrary.

[Eric Smith <eric+python-dev at trueblade.com>]:
> I don't see it as a big problem.  You can now use any prefix you want,
> instead of the hard coded values that # supplied.

based on both your replies, it sounds like it's going away!  :-)
no, i don't have a problem with it. however, it'd be nice to put
something about this in the PEP in case anyone else wonders/asks.


>> print format(10.0, "7.3g")

[Nick Coghlan <ncoghlan at gmail.com>]:
> It works fine as written in 2.x :)
> (but, yes, you're right that as a 3000-series PEP, 3101 should probably
> treat print() as a function in its examples)

[Eric Smith <eric+python-dev at trueblade.com>]:
> Fixed in r63786.  Thanks for catching it.  There was another print()
> function already in the PEP, so clearly the intent was to be 3.0 compliant.

another 2 suggestions then (only pick one):

1. if both str.format() and format() are going to be backported to
    2.x, there should be an example of it there too (see below where
    i'm also taking an additional liberty of changing "g" to "f"
    which i use more and give another number as an example):

    2.x:
    >>> print format(10.8765, '7.2f')
      10.88

    3.x:
    >>> print(format(10.8765, '7.2f'))
      10.88

2. drop the print altogether, esp. since this is about strings.

    >>> format(10.8765, '7.2f')
    '  10.88'

cheers,
-wesley


More information about the Python-3000 mailing list