Another way to do this is by using the bitstring module:

>>> from bitstring import BitArray
>>> input_str = '0xff'
>>> c = BitArray(hex=input_str)
>>> c.bin
'0b11111111'

And if you need to strip the leading 0b:

>>> c.bin[2:]
'11111111'

The bitstring module isn't a requirement, as jcollado's answer shows, but it has lots of performant methods for turning input into bits and manipulating them. You might find this handy (or not), for example:

>>> c.uint
255
>>> c.invert()
>>> c.bin[2:]
'00000000'

etc.

Answer from Alex Reynolds on Stack Overflow
🌐
Python.org
discuss.python.org › python help
Printing binary strings - Python Help - Discussions on Python.org
April 8, 2025 - Hello all, Why is the Python 3 interpreter printing byte strings by decoding bytes as UTF-8 (perhaps actually only true for the ASCII analogue codepoints), instead of printing \xNN values? Am I missing something? Th…
Discussions

Whats a good way of writing an -int- as BYTES into a binary file (and later, reading it in too) ?
There's many ways. Here's the first one that comes to mind for me: filename = Path('myfile.bin') # assuming you want 4-byte (32-bit) unsigned integers with open(filename, 'wb') as f: f.write((35400).to_bytes(4, 'big')) f.write((364).to_bytes(4, 'big')) with open(filename, 'rb') as f: print(int.from_bytes(f.read(4), 'big')) print(int.from_bytes(f.read(4), 'big')) More on reddit.com
🌐 r/learnpython
7
9
February 7, 2022
Python Byte doesn't print binary - Stack Overflow
When I print a program such as this in Python: x = b'francis' The output is b'francis'. If bytes is in 0's and 1's why is it not printing it out? More on stackoverflow.com
🌐 stackoverflow.com
python - How to print binary file as bytes? - Stack Overflow
I did >>> b0 = open('file','rb') Then >>> b0.read(10) gives b'\xb8\xaaK\x1e^J)\xab_I' How can I get things printed all as pure hex bytes? I want b'\xb8\xaa\x4b\x1e\x5e\x4a\x... More on stackoverflow.com
🌐 stackoverflow.com
May 24, 2017
How can I convert bytes object to decimal or binary representation in python? - Stack Overflow
I wanted to convert an object of type bytes to binary representation in python 3.x. For example, I want to convert the bytes object b'\x11' to the binary representation 00010001 in binary (or 17 in More on stackoverflow.com
🌐 stackoverflow.com
🌐
Theunterminatedstring
theunterminatedstring.com › python-bits-and-bytes
Python Bits and Bytes - The Unterminated String
May 19, 2018 - This object is immutable and can store raw binary values within the range 0 to 255. It’s constructor is the aptly named bytes(). There are several different ways to initialise a bytes object: >>> bytes((1,2,3)) b'\x01\x02\x03' >>> bytes("hello", "ascii") b'hello'
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-bytes-to-bits-in-python
Convert Bytes To Bits in Python - GeeksforGeeks
July 23, 2025 - For example , a byte like 0xAB (which is 171 in decimal) would be represented as '10101011' in binary. Let’s explore a few techniques to convert bytes to bits in Python. This method converts a byte sequence to an integer using int.from_bytes() and then calculates the number of bits required to represent that integer using .bit_length(). Python · a = b'\x01\x23\x45\x67\x89\xAB\xCD\xEF' b = int.from_bytes(a, byteorder='big').bit_length() print(b) Output ·
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert python bytes to binary string
5 Best Ways to Convert Python Bytes to Binary String - Be on the Right Side of Change
February 23, 2024 - The challenge is to transform a bytes object like b'\xf0\xf1' into its binary representation, such as ‘1111000011110001’. This article guides you through five efficient ways to achieve this conversion. This method uses Python’s built-in bin() function to convert each byte in a bytes object to a binary string, and then it concatenates the binary strings after removing the ‘0b’ prefix added by the bin() function. ... bytes_data = b'\xf0\xf1' binary_string = ''.join(format(byte, '08b') for byte in bytes_data) print(binary_string)
🌐
YouTube
youtube.com › alltech
convert byte to binary string in python 😀 - YouTube
Script in Python that prints the binary of a byte.Support this channel, become a member:https://www.youtube.com/channel/UCBGENnRMZ3chHn_9gkcrFuA/join🔥 Udemy...
Published: August 23, 2016
Views: 4K
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › python-bytes-to-string-code-examples
Python Bytes to String – How to Convert a Str to Bytes and Back Again
April 16, 2024 - We stored this conversion in the ... and got a binary value: print(byte_data[0]) # 72. You can use the decode() method to convert bytes to a string in Python....
🌐
GeeksforGeeks
geeksforgeeks.org › working-with-binary-data-in-python
Working with Binary Data in Python - GeeksforGeeks
June 22, 2020 - This means that before Python3 we could treat a set of bytes as a string and work from there, this is not the case now, now we have a separate data type, called bytes. This data type can be briefly explained as a string of bytes, which essentially means, once the bytes data type is initialized it is immutable. ... bytestr = bytes(b'abc') # initializing a string with b # makes it a binary string print(bytestr) print(bytestr[0]) bytestr[0] = 97
Top answer
1 of 4
6

You seem to be fundamentally confused, in a very common way. The data itself is a distinct concept from its representation, i.e. what you see when you attempt to print it out or otherwise display it. There may be multiple ways to represent the same data. This is just like how if I write 23 (in decimal) or 0x17 (hexadecimal) or 0o27 (octal) or 0b10111 (binary) or twenty-three (English), I am talking about the same number.

At some lower level below Python, everything is bytes, and each byte consists of bits; but it is not correct to say that the bytes "are in" 0s and 1s - just like how it is not correct to say that the number twenty-three "is in" decimal digits (or hexadecimal, octal or binary ones, or in English text characters).

The symbols 0 and 1 are just pictures that we draw on a screen to represent the state of those bits - if we choose to represent them individually. Sometimes, we choose larger groupings, and assign different symbols to various combinations of states. For example, we may interpret multiple bits as a single integer value in binary; or (using Unicode) we might further interpret that number as a "code point" (most of these are text characters; some are control characters, or portions of text characters).

A Python bytes object is a wrapper for a "raw" sequence of bytes. When you display it, Python uses a representation where each byte (grouping of 8 bits) corresponds to one or more symbols: bytes whose corresponding integer value is between thirty-two and one hundred twenty-six (inclusive) are (for historical reasons) represented using individual text characters (following the so-called ASCII encoding), while others are represented with a four-character "escape sequence" beginning with \x and followed by the hexadecimal representation of the number.

2 of 4
3

From python docs:

bytes and bytearray objects are sequences of integers (between 0 and 255), representing the ASCII value of single bytes.

So they are sequence of integers which represents ASCII values.

For conversion you can use:

import sys
int.from_bytes(b'\x11', byteorder=sys.byteorder)  # => 17
bin(int.from_bytes(b'\x11', byteorder=sys.byteorder))  # => '0b10001'
🌐
Real Python
realpython.com › python-bytes
Bytes Objects: Handling Binary Data in Python – Real Python
March 5, 2025 - This method works best when your ... in your Python source code in literal form. Such a literal could serve as a constant meant to represent a binary file signature, also known as a “magic number.” · In this case, the byte values stored in your bytes object happen to have a human-readable representation because they correspond to well-known character codes: To embed non-printable values, you’ll ...
🌐
Medium
medium.com › @mksdev.art › handling-binary-data-in-python-bytes-bytearray-memoryview-34ba793ace4c
Handling Binary Data in Python (bytes, bytearray, memoryview) | by MKSDEV.ART | Medium
March 18, 2025 - memoryview can be modified when created from a mutable object such as bytearray. ba = bytearray(b'Hello') mv = memoryview(ba) mv[0] = 77 print(ba) # Output: bytearray(b'Mello') Press enter or click to view image in full size · bytes provides an immutable sequence of bytes. bytearray is a mutable sequence that allows modifications. memoryview provides a view object for efficient manipulation of binary data. Python ·
🌐
Python Morsels
pythonmorsels.com › representing-binary-data-with-bytes
Representing binary data with bytes - Python Morsels
December 27, 2021 - In Python, strings are used to represent text and bytes are used to represent binary data. If you end up with bytes representing text, you can decode them to get a string instead.
🌐
Bogotobogo
bogotobogo.com › python › python_bits_bytes_bitstring_constBitStream.php
Python Tutorial: bits, bytes, bitstring, and ConstBitStream - 2020
int:n n bits as a signed integer. uint:n n bits as an unsigned integer. hex:n n bits as a hexadecimal string. bin:n n bits as a binary string. bits:n n bits as a new bitstring. bytes:n n bytes as bytes object.
🌐
Agr0 Hacks Stuff
agrohacksstuff.io › posts › working-with-bytecode-in-python
Working with Bytecode in Python | Agr0 Hacks Stuff
April 5, 2024 - The difference in how they are used relate to whether you ask for a single integer or a list of integers, where a list of integers will return the bytestring of exactly those numbers in raw binary form, here represented as hexidecimal: Note: \x0a in ascii is LF (Line Feed), or here represented as \n. Additionally, for those that aren’t as familiar with Python, a string denoted with the b prefix is a bytestring.
🌐
Bytes
bytes.com › home › forum › topic › python › how to print a file in binary mode
How to print a file in binary mode - Post.Byes
October 22, 2006 - I need print a file in binary mode . > f = f.open('python. jpg','rb') bytes = f.read() f.close() > print(bytes) > I can't get any binary code. What do you mean by "binary code"? If you use ``print repr(bytes)`` everything outside ASCII will be printed as escape sequence.
🌐
Mimo
mimo.org › glossary › python › bytes
Python Bytes: Syntax, Usage, and Examples
Python bytes are sequences of raw 8-bit values. You use them when dealing with non-text data or when you want to encode text in a specific binary format, such as UTF-8 or ASCII.