Python: bin() function
bin() function
The bin() function is used to convert an integer number to a binary string. The result is a valid Python expression.
Syntax:
bin(x)
Version:
(Python 3.2.5)
Note: If x is not a Python int object, it has to define an __index__() method that returns an integer.
Return value:
integer.
Example: Python bin() function - Convert integer to binary
Output:
Original number: 10 Binary string: 0b1010 Original number: -10 Binary string: -0b1010
Example: Python bin() function - Convert integer to binary without sign prefix
Output:
Original number: 10 10 Binary string: 00001010 Original number: -10 -10 Binary string: 00001010
Example: Convert an integer to binary without using bin() function
Output:
000001010 000001000 001101110
Python Code Editor:
Previous: ascii()
Next: bool()
Test your Python skills with w3resource's quiz
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics