diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index ef4176a..a385002 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -80,8 +80,27 @@ are always available. They are listed here in alphabetical order. .. function:: bin(x) - Convert an integer number to a binary string. The result is a valid Python - expression. If *x* is not a Python :class:`int` object, it has to define an + Convert an integer number to a binary string prefixed with "0b". + The result is a valid Python expression, for example: + + >>> bin(3) + '0b11' + >>> bin(-10) + '-0b1010' + + It can also be done by using the expression: + + >>> "{0:#b}".format(3) + '0b11' + + If prefix "0b" is not desired, you can use either of the following ways: + + >>> "{0:b}".format(3) + '11' + >>> format(3, 'b') + '11' + + If *x* is not a Python :class:`int` object, it has to define an :meth:`__index__` method that returns an integer. @@ -634,6 +653,26 @@ are always available. They are listed here in alphabetical order. >>> hex(-42) '-0x2a' + It can also be done by using the expression: + + >>> "%#x" % 255 + '0xff' + + If you want to convert an integer number to an uppercase hexadecimal string + prefixed with "0X": + + >>> "%#X" % 255 + '0XFF' + + If prefix "0x" is not desired, you can use either of the following ways: + + >>> format(255, 'x') + 'ff' + >>> "{0:x}".format(255) + 'ff' + >>> f'{255:x}' + 'ff' + If x is not a Python :class:`int` object, it has to define an __index__() method that returns an integer. @@ -865,8 +904,25 @@ are always available. They are listed here in alphabetical order. .. function:: oct(x) - Convert an integer number to an octal string. The result is a valid Python - expression. If *x* is not a Python :class:`int` object, it has to define an + Convert an integer number to an octal string prefixed with "0o". The result + is a valid Python expression, for example: + + >>> oct(8) + '0o10' + + It can also be done by using the expression: + + >>> "{0:#o}".format(8) + '0o10' + + If prefix "0o" is not desired, you can use either of the following ways: + + >>> format(8, 'o') + '10' + >>> "%o" % 8 + '10' + + If *x* is not a Python :class:`int` object, it has to define an :meth:`__index__` method that returns an integer.