STINNER Victor added the comment: Ezio Melotti added the comment:
I can think of 3 possible solutions:
1) keep the examples but condense them so that they don't take so much space:
n = 255 f'{n:#x}', format(n, '#x'), '%#x' % n ('0xff', '0xff', '0xff') f'{n:x}', format(n, 'x'), '%x' % n ('ff', 'ff', 'ff') f'{n:X}', format(n, 'X'), '%X' % n ('FF', 'FF', 'FF')
Hum. It's not easy to read these complex formatting strings when they are written like that.
or
'%#x' % 255, '%x' % 255, '%X' % 255 ('0xff', 'ff', 'FF') format(255, '#x'), format(255, 'x'), format(255, 'X') ('0xff', 'ff', 'FF') f'{255:#x}', f'{255:x}', f'{255:X}' ('0xff', 'ff', 'FF')
I really prefer when the same kind of the formating strings are written on the same line. I really like this example. Short, obvious, easy to read. I have a prefererence for an example using a variable name rather than a number literal. It's more common to manipulate variables than number literals. If you use a variable, please use a variable name longer than "n" to get more readable example. Otherwise, it's not obvious what is in the variable name in "{n:x}": is "n" the variable? is "x" the variable? In short, I suggest this example:
value = 255 '%#x' % value, '%x' % value, '%X' % value ('0xff', 'ff', 'FF') format(value, '#x'), format(value, 'x'), format(value, 'X') ('0xff', 'ff', 'FF') f'{value:#x}', f'{value:x}', f'{value:X}' ('0xff', 'ff', 'FF')
Note: Ezio, do you prefer format(value, 'x) for '{:x}'.format(value)?
2) add a direct link to https://docs.python.org/3/library/string.html#format-examples where there are already some examples (more can be added if needed);
IMHO it's ok to add formatting examples to bin/hex/oct. Using your compact example, it's not going to make the doc too long. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue26506> _______________________________________