To convert an integer to a binary string in Python, use the built-in bin() function. It returns a string prefixed with '0b' to indicate binary format.

num = 10
binary = bin(num)
print(binary)  # Output: '0b1010'

To remove the '0b' prefix, use string slicing:

binary_no_prefix = bin(num)[2:]
print(binary_no_prefix)  # Output: '1010'

For more control over formatting (like padding with leading zeros), use format() or f-strings:

# Using format()
binary_padded = format(num, '08b')  # 8-bit binary with leading zeros
print(binary_padded)  # Output: '00001010'

# Using f-strings
binary_padded = f'{num:08b}'
print(binary_padded)  # Output: '00001010'

Note: bin() works only with integers. For negative numbers, Python uses two's complement notation, so the output includes a negative sign (e.g., -0b1010 for -10).

Python's string format method can take a format spec.

>>> "{0:b}".format(37)
'100101'

Format spec docs for Python 2

Format spec docs for Python 3

Answer from Tung Nguyen on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-binary-list-to-integer
Python - Binary list to integer - GeeksforGeeks
July 11, 2025 - This is the most efficient method. By joining the binary list into a string and using the built-in int() function with a base of 2, we can directly convert the binary representation into its integer equivalent.
Discussions

How to convert an integer into a binary number without the specific function?
The idea is to repeatedly divide your number by 2, keeping track of the remainder each time, eg. def to_binary(n): assert n >= 0, 'Must be positive' binary = '' while n: # while n is not 0 n, r = divmod(n, 2) # the same as: n, r = n//2, n%2 binary = str(r) + binary # prepend the remainder to the output return binary or '0' # if the string is still empty, n must have been 0 More on reddit.com
🌐 r/learnpython
5
0
September 6, 2021
python - Convert base-2 binary number string to int - Stack Overflow
I'd simply like to convert a base-2 binary number string into an int, something like this: >>> '11111111'.fromBinaryToInt() 255 Is there a way to do this in Python? More on stackoverflow.com
🌐 stackoverflow.com
Converting integer to binary in Python - Stack Overflow
Explore Stack Internal ... Sign up to request clarification or add additional context in comments. ... The first 0 means the 0th argument to format. After the colon is the formatting, the second 0 means zero fill to 8 spaces and b for binary 2012-05-02T09:39:58.167Z+00:00 ... @Aif: Also, have a look at the standard documentation docs.python... More on stackoverflow.com
🌐 stackoverflow.com
I have a byte, I want to extract the bits (from MSB to LSB). What is the 'Python' way to do this (use a bytearray)?

I would use something like the following, but I'm not sure python is the best language for doing this sort of bit manipulation in. There are some C-based libraries that implement this you could look at.

output = [input & 1<<x > 0 for x in range(input.bit_length())]
More on reddit.com
🌐 r/learnpython
13
12
February 16, 2012
🌐
FavTutor
favtutor.com › blogs › binary-string-to-int-python
Convert Binary String to Int in Python (with code)
September 12, 2023 - ... # initialize a binary string bString = "10101" # conversion value = int(bString, 2) # display the converted value print(value) ... In Python, we may use the bin() function to convert a string to binary.
🌐
GeeksforGeeks
geeksforgeeks.org › python › integer-to-binary-string-in-python
Integer to Binary String in Python - GeeksforGeeks
July 23, 2025 - Input : 77 Output : 0b1001101 Explanation: Here, we have integer 77 which we converted into binary string · bin() function is the easiest way to convert an integer into a binary string.
🌐
Vultr Docs
docs.vultr.com › python › built-in › bin
Python bin() - Convert Integer to Binary | Vultr Docs
September 27, 2024 - Select a negative integer for conversion. Use the bin() function on this integer. ... negative_number = -14 binary_representation = bin(negative_number) print(binary_representation) Explain Code · For negative numbers, Python ...
🌐
Codemia
codemia.io › knowledge-hub › path › convert_int_to_binary_string_in_python
Convert int to binary string in Python
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
Medium
medium.com › @gitauharrison › binary-representation-of-integers-in-python-bfd2f4d3c6e5
Binary Representation of Integers in Python | by Gitau Harrison | Medium
September 14, 2022 - The 0 will be dynamically replaced by the parameter in format() . The b defines how you want the conversion to happen, in this case, it will be binary. A more modern way of formatting strings as of Python3.6 is the use of “formatted string literals,” also known as f-strings. Not only are they more readable, more concise, and less prone to error than other ways of formatting, but they are also faster! ... Now that you know how to convert an integer into its binary representation using Python, try to create a function that takes an integer as input, converts it to binary, and prints out the number of occurrences of the value 0.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › how to convert an integer into a binary number without the specific function?
r/learnpython on Reddit: How to convert an integer into a binary number without the specific function?
September 6, 2021 -

Hello,

I'm fairly new and I wanted to challenge myself by trying to convert an integer into a binary number (up until 256). I'd use the functions like if, elif else and while. I'd also use some math and some string operations. I've tried, but it feels like I'm stuck.

Here's what I have so far:

def convert_number_into_binary(number):

assert number <= 256

binary = ""

for i in range(8,-1):

while number != 0:

if number - 2 ** i == 0:

binary = "0" * (8 - i) + str(10 ** i)

return binary

elif number - 2 ** i != 0:

if 2**i > number: binary += "0"

elif 2**i < number: number -= 2**i binary += "1"

sorry for the lack of indents, I don't know how to transfer the code properly from Python to Reddit.

Thanks in advance

🌐
AskPython
askpython.com › home › integer to binary string in python
Integer to Binary String in Python - AskPython
March 7, 2023 - In the output of example 2, you ... So, it becomes easy to know the type of integer is positive or negative using the bin() function in python....
🌐
DataCamp
datacamp.com › tutorial › python-data-type-conversion
Python Data Type Conversion: A Guide With Examples | DataCamp
February 16, 2025 - In Python, you can simply use the bin() function to convert from a decimal value to its corresponding binary value. Similarly, the int() function to convert a binary to its decimal value.
🌐
datagy
datagy.io › home › python posts › python strings › python: int to binary (convert integer to binary string)
Python: Int to Binary (Convert Integer to Binary String) • datagy
December 19, 2022 - The Python bin() function is short for binary and allows us to convert an integer to a binary string, which is prefixed by '0b'. In later section, you’ll learn how to convert the integer using Python without the prefix.
🌐
Python Pool
pythonpool.com › home › blog › python int to binary | integer to binary conversion
Python int to Binary | Integer to Binary Conversion - Python Pool
June 14, 2021 - Most commonly used function to convert Python int to binary is bin().num2 = 7print('The binary equivalent of 1 is:', bin(num1))
🌐
Quora
quora.com › How-do-I-convert-integers-to-binary-in-Python-3
How to convert integers to binary in Python 3 - Quora
Answer (1 of 7): Use the function ‘bin(x)’ , where x is to be taken as the decimal input and it outputs the binary value as a string: For eg: bin(20) = '0b10100'
🌐
Reintech
reintech.io › blog › python-bin-method-tutorial
Python: Working with the bin() Method | Reintech media
January 20, 2026 - Python's arbitrary-precision integers mean bin() handles numbers of any size without overflow concerns, unlike fixed-width integer types in languages like C or Java. Python represents negative binary numbers using a simple negative sign prefix rather than two's complement notation: negative_value = -10 binary_rep = bin(negative_value) print(binary_rep) # Output: -0b1010 · This representation differs from how negative numbers are stored at the machine level. For two's complement representation (used in actual computer memory), you'll need to manually implement the conversion:
🌐
AskPython
askpython.com › home › converting base-2 binary number strings to integers in python
Converting Base-2 Binary Number Strings to Integers in Python - AskPython
March 26, 2023 - We can use the int() built-in function to convert a string literal into an integer in python. Let’s look at how we can implement this : #converting a base-2 number string into an integer.
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.3 documentation
4 weeks ago - Return the number of bits necessary to represent an integer in binary, excluding the sign and leading zeros:
🌐
w3resource
w3resource.com › python-exercises › python-basic-exercise-140.php
Python: Convert an integer to binary keep leading zeros - w3resource
May 17, 2025 - Converting an integer to an n-bit binary number results in its binary representation containing leading zeros up to length n. For example, to convert the integer 5 to a 6-bit binary results in 000101.
🌐
Mkyong
mkyong.com › home › python › python – how to convert int to a binary string?
Python - How to convert int to a binary string? - Mkyong.com
June 2, 2020 - print(bin(1)) # 0b1 print(bin(-1)) # -0b1 print(bin(10)) # 0b1010 print(bin(-10)) # -0b1010 print("{0:b}".format(10)) # 1010 print("{0:#b}".format(10)) # 0b1010 , with 0b prefix print("{0:b}".format(10).zfill(8)) # 00001010 , pad zero, show 8 bits print(format(10, "b")) # 1010 print(format(10, "#b")) # 0b1010, with 0b prefix print(format(10, "b").zfill(16)) # 0000000000001010, pad zero, show 16 bits # with hex, oct bin # int: 10; hex: a; oct: 12; bin: 1010 result = "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(10) print(result) # with 0x, 0o, or 0b as prefix: # int: 10; hex: 0xa; oct