[New-bugs-announce] [issue38657] String format for hexadecimal notation breaks padding with alternative form

Pete Wicken report at bugs.python.org
Thu Oct 31 16:36:00 EDT 2019


New submission from Pete Wicken <petewicken at gmail.com>:

When formatting an integer as a hexadecimal value, the '#' alternate form modifier inserts a preceding '0x'. 
If this is used in combination with padding modifiers, the '0x' is counted as part of the overall width, which does not feel like the natural behaviour as extra calculation is required to get the correct post '0x' precision.

Example:

In [7]: f'{num:04x}'
Out[7]: '0800'

In [8]: f'{num:#04x}'
Out[8]: '0x800'

To get the hexadecimal representation padded to 4 digits, you have to account for the preceding 0x:

In [10]: f'{num:#06x}'
Out[10]: '0x0800'

----------
messages: 355767
nosy: Wicken
priority: normal
severity: normal
status: open
title: String format for hexadecimal notation breaks padding with alternative form
type: behavior
versions: Python 3.7, Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38657>
_______________________________________


More information about the New-bugs-announce mailing list