Built-in Functions and Types
bin()
Format an integer as base-2 text with the prefix 0b. Negative inputs place the minus sign before the prefix rather than emitting a fixed-width two’s-complement bit pattern. Objects implementing __index__() are accepted; floats are not implicitly truncated. Use format() if a prefix-free or width-controlled representation is needed.
Formal Syntax & Structure
python syntaxpython
bin(integer, /)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| integer | int or object supporting __index__ | Integer-like value; positional only. |
Return Value
str
A base-2 representation with 0b and an optional leading minus sign.
Working Example
Example Snippetpython
print(bin(45))print(bin(-5))