bin(): converts an integer to a binary string prefixed with "0b"

bin(number)
number :input number in decimal
Return the binary number as string.

Examples

num=5
print(bin(num)) # 0b101
num=47
print(bin(num)) # 0b101111
In our binary output, 0b is always prefixed. As the output is a string we can remove it like this
num=47
num1=bin(num)
print(num1[2:]) # 101111
Without using bin() and by using format()
num=47
print(format(num,'b')) # 101111

Example 1: Convert Negative Integers to Binary

print(bin(-10)) # Output: '-0b1010'

Example 2: Binary Conversion for Data Encoding

data = [65, 66, 67]
binary_data = [bin(x) for x in data]
print(binary_data)  # Output: ['0b1000001', '0b1000010', '0b1000011']

Example 3: Converting Large Numbers to Binary

large_num = 1024
print(bin(large_num))  
# Output: '0b10000000000'

Example 4: Removing the '0b' Prefix from the Binary String

num = 15
binary_str = bin(num)[2:]  
print(binary_str)  
# Output: '1111'

Example 5: Binary Addition

num1 = 0b1010  # Binary for 10
num2 = 0b1100  # Binary for 12
result = num1 + num2

print(bin(result))  
# Output: '0b10110' (binary for 22)

print(int(result))  
# Output: 22

All Built in Functions in Python
max() Bitwise operators using bin() int() float() format()


Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer