Complete guide to Python's bin function covering integer conversion, binary representation, and practical examples.
Last modified April 11, 2025
This comprehensive guide explores Python’s bin function, which converts integers to their binary string representation. We’ll cover basic usage, formatting options, and practical applications in computing.
The bin function converts an integer number to a binary string prefixed with “0b”. The result is a valid Python expression that could be evaluated back to the original number.
Key characteristics: accepts integers (positive or negative), returns a string, and always includes the “0b” prefix. For non-integers, it raises TypeError.
Here’s simple usage with different integers showing how bin handles positive, negative, and zero values.
basic_bin.py
print(bin(5)) # 0b101 print(bin(10)) # 0b1010
print(bin(-5)) # -0b101 print(bin(-10)) # -0b1010
print(bin(0)) # 0b0
This example shows bin with different integer values. Positive numbers get a “0b” prefix, while negatives get “-0b”. Zero converts to “0b0”.
The binary representation shows the base-2 form of the number. Note that negative numbers are represented with a minus sign, not two’s complement.
This example demonstrates how to convert between binary, decimal, and hexadecimal representations using bin, int, and hex.
base_conversion.py
number = 42
binary = bin(number) print(binary) # 0b101010
decimal = int(binary, 2) print(decimal) # 42
print(hex(number)) # 0x2a
The example shows full conversion cycle between decimal and binary. The int function can parse binary strings when given base 2.
This demonstrates Python’s consistent base conversion functions: bin for binary, hex for hexadecimal, and int for parsing.
You can make custom objects work with bin by implementing the index special method. This example creates a custom number.
custom_bin.py
class CustomNumber: def init(self, value): self.value = value
def __index__(self):
return self.value
def __repr__(self):
return f"CustomNumber({self.value})"
num = CustomNumber(15) print(bin(num)) # 0b1111
The CustomNumber class implements index to return its value. When we call bin on an instance, Python uses this method.
This pattern is useful for custom numeric types that should support binary representation while maintaining their own internal state.
The bin function raises TypeError when used with non-integer types. This example shows proper error handling.
errors.py
try: print(bin(3.14)) except TypeError as e: print(f"Error: {e}") # ‘float’ object cannot be interpreted as an integer
class NoIndex: pass
try: print(bin(NoIndex())) except TypeError as e: print(f"Error: {e}") # ‘NoIndex’ object cannot be interpreted as an integer
These examples demonstrate bin’s behavior with unsupported types. Floats and objects without index raise TypeError.
To make a class work with bin, implement index as shown in the previous example.
This example shows practical string manipulation of binary representations, including stripping the prefix and padding with zeros.
string_manipulation.py
number = 42
binary_str = bin(number)[2:] print(binary_str) # ‘101010’
padded = binary_str.zfill(8) print(padded) # ‘00101010’
bit_count = binary_str.count(‘1’) print(bit_count) # 3
This demonstrates common operations on binary strings. We remove the “0b” prefix, pad with zeros for fixed-width display, and count set bits.
These techniques are useful for bit manipulation, hardware interfacing, and low-level programming tasks where binary representation matters.
Use for readability: Prefer bin over manual conversion
Implement index: For custom types needing binary representation
Handle prefix: Remember to strip “0b” when needed
Consider format: Use f-strings for custom binary formatting
Document behavior: Clearly document binary representation
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.