ID EN
Built-in Functions

bin

Python 3.11

Convert an integer number to a binary string prefixed with “0b”. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer. Some examples:

Syntax

PYTHON
bin(x)

Examples

Example 1
PYTHON
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
Example 2
PYTHON
>>> format(14, 'b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:b}', f'{14:b}'
('0b1110', '1110')

See Also

int __index__() format()