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.

classification
Title: Clarify numeric padding behavior in string formatting
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: Wicken, eric.smith, mark.dickinson, miss-islington, serhiy.storchaka, veky
Priority: normal Keywords: patch

Created on 2019-10-31 20:36 by Wicken, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 17036 merged Wicken, 2019-11-03 12:42
PR 18587 merged miss-islington, 2020-02-21 05:53
PR 18588 merged miss-islington, 2020-02-21 05:53
Messages (11)
msg355767 - (view) Author: Pete Wicken (Wicken) * Date: 2019-10-31 20:36
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'
msg355794 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-11-01 07:22
Yes, the width is the width of the formatted value, not the number of digits.

What is your proposition?
msg355814 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-11-01 12:20
int.__format__ inherits this from %-formatting, which inherits it from C's printf.

There's no way we're going to change this at this point: the breakage would be too great. So, I'm going to reject this.
msg355825 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-11-01 17:21
Now that I re-read this, maybe it was a documentation request, not a functional change? I'd be okay with documenting the existing behavior, so I'll re-open this and change the type. Patches welcome.
msg355834 - (view) Author: Vedran Čačić (veky) * Date: 2019-11-01 18:42
The width doesn't mean "the number of bits", it means "the width of the field". In every other case too:

* when we format negative numbers, width includes the minus sign
* when we format decimal numbers, width includes decimal point (or comma)
* when we format strings with !r, width includes the quotes

So, not only would it break too much code, but it would actually be inconsistent to formatting all other types currently.
msg355870 - (view) Author: Pete Wicken (Wicken) * Date: 2019-11-02 14:23
Given the comments above I appreciate that this is actually due to the padding being the total field width rather than the padding of the digits themselves. Having revised the documentation again, I believe this following line is explaining it:

"When no explicit alignment is given, preceding the width field by a zero ('0') character enables sign-aware zero-padding for numeric types. This is equivalent to a fill character of '0' with an alignment type of '='."

(https://docs.python.org/3.8/library/string.html)

I initially read "sign-aware zero-padding for numeric types" to mean the padding would not blindly prepend, and would take into account any signs and pad after (hence initially making this a bug). So maybe as suggested above we should explicitly mention the padding is the total number of characters in the field, rather than just the numbers.

I can look into adding this soon and see what you all think.
msg355880 - (view) Author: Vedran Čačić (veky) * Date: 2019-11-02 18:33
It seems that you're confusing two things that really don't have much in common.

* (field) width is a _number_, saying how many characters (at least) should the formatted output take.
* padding is a bool (or maybe a char), saying what should be put inside the leftover space if the default formatted output is shorter than the width

The padding is not the width, and the width is not the padding. Once you start to differentiate those two things, I'm convinced all your confusions will disappear.
msg362380 - (view) Author: miss-islington (miss-islington) Date: 2020-02-21 05:53
New changeset 424e5686d82235e08f8108b8bbe034bc91421689 by Pete Wicken in branch 'master':
bpo-38657: Clarify numeric padding behaviour in string formatting (GH-17036)
https://github.com/python/cpython/commit/424e5686d82235e08f8108b8bbe034bc91421689
msg362381 - (view) Author: miss-islington (miss-islington) Date: 2020-02-21 06:06
New changeset 09db1da63f866afff8a64ae3c60acdcd6bc80501 by Miss Islington (bot) in branch '3.7':
bpo-38657: Clarify numeric padding behaviour in string formatting (GH-17036)
https://github.com/python/cpython/commit/09db1da63f866afff8a64ae3c60acdcd6bc80501
msg362382 - (view) Author: miss-islington (miss-islington) Date: 2020-02-21 06:06
New changeset a2075121217e809c9a5511f6ca225c12f340de0c by Miss Islington (bot) in branch '3.8':
bpo-38657: Clarify numeric padding behaviour in string formatting (GH-17036)
https://github.com/python/cpython/commit/a2075121217e809c9a5511f6ca225c12f340de0c
msg362507 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-02-23 13:24
It looks as though this has been addressed, and can be closed.
History
Date User Action Args
2022-04-11 14:59:22adminsetgithub: 82838
2020-02-23 13:24:35mark.dickinsonsetstatus: open -> closed
resolution: fixed
messages: + msg362507

stage: patch review -> resolved
2020-02-21 06:06:21miss-islingtonsetmessages: + msg362382
2020-02-21 06:06:04miss-islingtonsetmessages: + msg362381
2020-02-21 05:53:35miss-islingtonsetpull_requests: + pull_request17958
2020-02-21 05:53:28miss-islingtonsetpull_requests: + pull_request17957
2020-02-21 05:53:19miss-islingtonsetnosy: + miss-islington
messages: + msg362380
2019-11-03 12:49:08eric.smithsettitle: String format for hexadecimal notation breaks padding with alternative form -> Clarify numeric padding behavior in string formatting
2019-11-03 12:42:00Wickensetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request16548
2019-11-02 18:33:35vekysetmessages: + msg355880
2019-11-02 14:23:44Wickensetmessages: + msg355870
2019-11-01 18:42:10vekysetnosy: + veky
messages: + msg355834
2019-11-01 18:21:41mark.dickinsonsetnosy: + mark.dickinson
2019-11-01 17:21:07eric.smithsetstatus: closed -> open
versions: + Python 3.9
messages: + msg355825

components: + Documentation
resolution: rejected -> (no value)
stage: resolved -> needs patch
2019-11-01 12:20:17eric.smithsetstatus: open -> closed

assignee: eric.smith

nosy: + eric.smith
messages: + msg355814
resolution: rejected
stage: resolved
2019-11-01 07:22:19serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg355794
2019-10-31 20:36:00Wickencreate