classification
Title: string.format() Unexpected output with numeric '#' prefix and 0 width
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 3.3, Python 2.7, Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Alex.Leone, eric.smith, r.david.murray
Priority: normal Keywords:

Created on 2010-12-05 23:40 by Alex.Leone, last changed 2010-12-06 03:17 by r.david.murray. This issue is now closed.

Messages (3)
msg123441 - (view) Author: Alex Leone (Alex.Leone) Date: 2010-12-05 23:40
When using the '#' to prefix a numeric argument in format() with a '0x' or others, the 0-width padding takes into account the '0x' characters.  This is unexpected - the 0-width should NOT take into account the prefix.

Current Behavior:
> "{0:#02x}".format(10)
'0xa'

Expected Output:
> "{0:#02x}".format(10)
'0x0a'

(note that the '0a' is two characters long, as it should be)
msg123443 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-12-06 00:01
I disagree that your expected output is how it should behave. I believe it's more likely that the user wants the entire field width specified. In addition, compatibility with %-formatting would dictate that we keep the current behavior.

 >>> '%#02x' % 10
'0xa'
msg123449 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-12-06 03:17
Eric, I'm assuming you just forgot to close this.  On the other hand, if you wanted a +1 from another dev, you've got it :)  Besides the considerations you mentioned, changing this would be a significant backward incompatibility, and is therefore pretty much ruled out at this point, no matter what anyone might want.
History
Date User Action Args
2010-12-06 03:17:13r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg123449

resolution: not a bug
stage: resolved
2010-12-06 00:01:04eric.smithsetnosy: + eric.smith
messages: + msg123443
2010-12-05 23:40:47Alex.Leonecreate