[New-bugs-announce] [issue8062] PEP 3101 string formatting missing hexadecimal separator _ for every 4 hex digits

steven Michalske report at bugs.python.org
Thu Mar 4 22:46:46 CET 2010


New submission from steven Michalske <smichalske at gmail.com>:

It is a common practice to separate hex digits with a "thousands" separator every 4 hex digits.

0x1234_abcd

Although python does not accept the _ as a thousands separator in hex notation, neither is the thousands separator in base 10


Snippet that prints hex thousands with a _ separator

number = 0xef5678abcd1234

def hex_thousands(number):
    txt="{0:X}".format(number)
    txt_out = ""
    i = range(-4,-1 * (len(txt) + 4), -4)
    ii = i[:]
    ii.insert(0, None)
    for (j, k) in zip(i, ii):
        if txt_out:
            txt_out = "_" + txt_out
        txt_out = txt[j:k] + txt_out
    return txt_out

print hex_thousands(number)

----------
messages: 100422
nosy: hardkrash
severity: normal
status: open
title: PEP 3101 string formatting missing hexadecimal separator _ for every 4 hex digits
versions: Python 2.7, Python 3.3

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8062>
_______________________________________


More information about the New-bugs-announce mailing list