[issue26506] hex() documentation: mention "%x" % int
STINNER Victor
report at bugs.python.org
Mon Mar 21 05:00:55 EDT 2016
STINNER Victor added the comment:
Serhiy Storchaka:
> The documentation for hex() doesn't look the bests place for examples of using string formatting. I think it is enough to add short references to corresponding formatting codes.
I like Manvi B's patch with many examples. It's hard to read formatting strings, it's hard to compute the result, so full examples are just more obvious.
I don't think that it hurts to add many formatting examples. I expect that most users will combine the result of bin/hex/oct with another string, so suggesting using formatting functions will probably help them to simplify the code.
For example,
print("x=", hex(x), "y=", hex(y))
can be written:
print("x=%#x y=%#x" % (x, y))
or
print("x={:#x} y={:#x}".format(x, y))
or
print(f"x={x:#x} y={y:#x}")
The first expression using hex() adds spaces after "=", but well, it's just to give a simple example. IMHO formatting strings are more readable.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26506>
_______________________________________
More information about the Python-bugs-list
mailing list