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

wesley chun wescpy at gmail.com
Fri May 30 01:34:39 CEST 2008


On 5/29/08, Eric Smith <eric+python-dev at trueblade.com> wrote:
> Marcin 'Qrczak' Kowalczyk wrote:
> > Except that it works incorrectly for negative numbers.

wow, that is a great point.  i didn't think of this either. it makes
it very inconvenient (see below) and makes it more difficult to say
we've completed replaced the '%' operator.


>  I can't really think of any other way that doesn't involve converting the
> number to a string and then operating on that, just to get the sign.

here's one way of doing it without converting to a string first (it's ugly too):

>>> i = -45
>>> '{0}0x{1:x}'.format('-' if i < 0 else '', abs(i))
'-0x2d'

thx for putting it (back) in,
-wesley


More information about the Python-3000 mailing list