New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarify numeric padding behavior in string formatting #82838
Comments
When formatting an integer as a hexadecimal value, the '#' alternate form modifier inserts a preceding '0x'. Example: In [7]: f'{num:04x}' In [8]: f'{num:#04x}' To get the hexadecimal representation padded to 4 digits, you have to account for the preceding 0x: In [10]: f'{num:#06x}' |
Yes, the width is the width of the formatted value, not the number of digits. What is your proposition? |
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. |
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. |
The width doesn't mean "the number of bits", it means "the width of the field". In every other case too:
So, not only would it break too much code, but it would actually be inconsistent to formatting all other types currently. |
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. |
It seems that you're confusing two things that really don't have much in common.
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. |
It looks as though this has been addressed, and can be closed. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: