This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients Manvi B, docs@python, eric.smith, ezio.melotti, serhiy.storchaka, vstinner, wolma
Date 2016-03-21.09:23:57
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1458552237.36.0.764739770991.issue26506@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2016-03-21 09:23:57vstinnersetrecipients: + vstinner, eric.smith, ezio.melotti, docs@python, serhiy.storchaka, wolma, Manvi B
2016-03-21 09:23:57vstinnersetmessageid: <1458552237.36.0.764739770991.issue26506@psf.upfronthosting.co.za>
2016-03-21 09:23:57vstinnerlinkissue26506 messages
2016-03-21 09:23:57vstinnercreate