Skip to content

Commit 67ba4fa

Browse files
manvi77Mariatta
authored andcommitted
bpo-26506: hex() documentation: mention %x % int (GH-2525)
1 parent 0653fba commit 67ba4fa

File tree

1 file changed

+52
-10
lines changed

1 file changed

+52
-10
lines changed

Doc/library/functions.rst

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,24 @@ are always available. They are listed here in alphabetical order.
8181

8282
.. function:: bin(x)
8383

84-
Convert an integer number to a binary string. The result is a valid Python
85-
expression. If *x* is not a Python :class:`int` object, it has to define an
86-
:meth:`__index__` method that returns an integer.
84+
Convert an integer number to a binary string prefixed with "0b". The result
85+
is a valid Python expression. If *x* is not a Python :class:`int` object, it
86+
has to define an :meth:`__index__` method that returns an integer. Some
87+
examples:
88+
89+
>>> bin(3)
90+
'0b11'
91+
>>> bin(-10)
92+
'-0b1010'
93+
94+
If prefix "0b" is desired or not, you can use either of the following ways.
95+
96+
>>> format(14, '#b'), format(14, 'b')
97+
('0b1110', '1110')
98+
>>> f'{14:#b}', f'{14:b}'
99+
('0b1110', '1110')
100+
101+
See also :func:`format` for more information.
87102

88103

89104
.. class:: bool([x])
@@ -635,16 +650,26 @@ are always available. They are listed here in alphabetical order.
635650

636651
.. function:: hex(x)
637652

638-
Convert an integer number to a lowercase hexadecimal string
639-
prefixed with "0x", for example:
653+
Convert an integer number to a lowercase hexadecimal string prefixed with
654+
"0x". If x is not a Python :class:`int` object, it has to define an
655+
__index__() method that returns an integer. Some examples:
640656

641657
>>> hex(255)
642658
'0xff'
643659
>>> hex(-42)
644660
'-0x2a'
645661

646-
If x is not a Python :class:`int` object, it has to define an __index__()
647-
method that returns an integer.
662+
If you want to convert an integer number to an uppercase or lower hexadecimal
663+
string with prefix or not, you can use either of the following ways:
664+
665+
>>> '%#x' % 255, '%x' % 255, '%X' % 255
666+
('0xff', 'ff', 'FF')
667+
>>> format(255, '#x'), format(255, 'x'), format(255, 'X')
668+
('0xff', 'ff', 'FF')
669+
>>> f'{255:#x}', f'{255:x}', f'{255:X}'
670+
('0xff', 'ff', 'FF')
671+
672+
See also :func:`format` for more information.
648673

649674
See also :func:`int` for converting a hexadecimal string to an
650675
integer using a base of 16.
@@ -878,10 +903,27 @@ are always available. They are listed here in alphabetical order.
878903

879904
.. function:: oct(x)
880905

881-
Convert an integer number to an octal string. The result is a valid Python
882-
expression. If *x* is not a Python :class:`int` object, it has to define an
883-
:meth:`__index__` method that returns an integer.
906+
Convert an integer number to an octal string prefixed with "0o". The result
907+
is a valid Python expression. If *x* is not a Python :class:`int` object, it
908+
has to define an :meth:`__index__` method that returns an integer. For
909+
example:
910+
911+
>>> oct(8)
912+
'0o10'
913+
>>> oct(-56)
914+
'-0o70'
915+
916+
If you want to convert an integer number to octal string either with prefix
917+
"0o" or not, you can use either of the following ways.
918+
919+
>>> '%#o' % 10, '%o' % 10
920+
('0o12', '12')
921+
>>> format(10, '#o'), format(10, 'o')
922+
('0o12', '12')
923+
>>> f'{10:#o}', f'{10:o}'
924+
('0o12', '12')
884925

926+
See also :func:`format` for more information.
885927

886928
.. index::
887929
single: file object; open() built-in function

0 commit comments

Comments
 (0)