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
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-bytes-to-bits-in-python
Convert Bytes To Bits in Python - GeeksforGeeks
July 23, 2025 - Explanation: sum(bin(byte).count('1') for byte in a) converts each byte in a to binary, counts the number of 1 bits and sums them up.
🌐
Real Python
realpython.com › python-bytes
Bytes Objects: Handling Binary Data in Python – Real Python
March 5, 2025 - Since bytes are closely related to strings, you often convert between the two data types, applying the correct character encoding. By the end of this tutorial, you’ll understand that: Python bytes objects are immutable sequences of unsigned bytes used for handling binary data.
🌐
Python
docs.python.org › 3 › library › struct.html
struct — Interpret bytes as packed binary data
If a count is not given, it defaults to 1. For packing, the byte string is truncated or padded with null bytes as appropriate to make it fit. For unpacking, the resulting bytes object always has exactly the specified number of bytes. As a special case, '0s' means a single, empty byte string (while '0c' means 0 characters). When packing, arguments of types bytes and bytearray are accepted. For the 'F' and 'D' format characters, the packed representation uses the IEEE 754 binary32 and binary64 format for components of the complex number, regardless of the floating-point format used by the platform.
🌐
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().
🌐
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 - A memoryview provides a view object that allows direct access to binary data without copying. # Creating memoryview from bytes b = b'Hello' mv = memoryview(b) ... 🚀 Python | AI/ML | Data Science Enthusiast. Sharing insights, tips, and practical solutions to simplify complex concepts.
🌐
Mimo
mimo.org › glossary › python › bytes
Mimo: The coding platform you need to learn Web Development, Python, and more.
The bytes type in Python represents a sequence of immutable byte values ranging from 0 to 255. This type is essential when you're working with binary data, such as reading or writing files in binary mode, processing network packets, or handling low-level protocols.
🌐
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 - Lastly, we printed the first character of the byte_data variable and got a binary value: print(byte_data[0]) # 72. You can use the decode() method to convert bytes to a string in Python.
Find elsewhere
🌐
Agr0 Hacks Stuff
agrohacksstuff.io › posts › working-with-bytecode-in-python
Working with Bytecode in Python | Agr0 Hacks Stuff
April 5, 2024 - It’s a cool idea that you can take any string of bytes, even a full executable file, and convert it into printable characters. You can store those printable characters anywhere, even in your clipboard! Just paste it through a decode method and presto, there’s your data. I will show how to do this in Python, but sometimes it’s more practical to use the base64 binary on Linux/MacOS systems, or for the more Microsoft-leaning among us, [Convert]::ToBase64String($SomeBytestringStoredAsVariable).
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-bytearray-to-bytes-in-python
Convert Bytearray To Bytes In Python - GeeksforGeeks
July 23, 2025 - Converting a bytearray to bytes in Python can be achieved through various methods, depending on the specific requirements of your code. The examples provided in this article cover different scenarios, from simple one-liners to more versatile approaches using modules like struct. Understanding these methods will empower you to handle binary ...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-int-to-bytes-in-python
How to Convert Int to Bytes in Python? - GeeksforGeeks
February 24, 2025 - Converting bytes to bits in Python involves representing each byte in its binary form, where each byte is composed of 8 bits. For example , a byte like 0xAB (which is 171 in decimal) would be represented as '10101011' in binary.
🌐
Vultr Docs
docs.vultr.com › python › built-in › bytes
Python bytes() - Create Byte Object | Vultr Docs
December 6, 2024 - The bytes() function in Python is essential for dealing with byte data, which is immutable sequences of single bytes. Often used in network communications, file handling, and interfacing with binary data, the bytes() function is a fundamental ...
🌐
Programiz
programiz.com › python-programming › methods › built-in › bytes
Python bytes()
Online Python Online JavaScript Online SQL Online Java Online HTML Online C Online C++ Online C# Online PHP Online Swift Online Kotlin Online TypeScript Online Go Online Rust Online Scala Online Dart Online R Online Ruby ... The bytes() method returns an immutable bytes object initialized with the given size and data. ... # convert string to bytes byte_message = bytes(message, 'utf-8') print(byte_message) # Output: b'Python is fun'
🌐
DataCamp
datacamp.com › tutorial › bytes-to-string-python
How to Convert Bytes to String in Python | DataCamp
June 12, 2024 - Python has a built-in bytes data structure, which is an immutable sequence of integers in the range 0 to 255. An integer within this range of 256 numbers can be represented using eight bits of data, which is equal to one byte.
🌐
Caam37830
caam37830.github.io › book › 00_python › bitsbytes.html
Bits, Bytes, and Numbers — Scientific Computing with Python
However, you will have to think about this for many algorithms in scientific computing. x = 5 # an integer type print(type(x)) x = 5.0 # float type print(type(x)) ... A bit is a 0/1 value, and a byte is 8 bits. Most modern computers are 64-bit architectures on which Python 3 will use 64-bits to represent numbers.
🌐
Python
docs.python.org › 3 › library › binary.html
Binary Data Services — Python 3.14.7 documentation
Other operations on binary data, specifically in relation to file formats and network protocols, are described in the relevant sections. Some libraries described under Text Processing Services also work with either ASCII-compatible binary formats (for example, re) or all binary data (for example, difflib). In addition, see the documentation for Python’s built-in binary data types in Binary Sequence Types — bytes, bytearray, memoryview.
🌐
GitHub
github.com › ofek › binary
GitHub - ofek/binary: Easily convert between binary and SI units (kibibyte, kilobyte, etc.). · GitHub
convert_units(n, unit=BYTE, to=None, si=False, exact=False) Converts between and within binary and decimal units. If no unit is specified, n is assumed to already be in bytes. If no to is specified, n will be converted to the highest unit possible.
Author: ofek
🌐
Python
docs.python.org › fr › dev › library › struct.html
struct --- Interpret bytes as packed binary data — Documentation Python 3.16.0a0
If a count is not given, it defaults to 1. For packing, the byte string is truncated or padded with null bytes as appropriate to make it fit. For unpacking, the resulting bytes object always has exactly the specified number of bytes. As a special case, '0s' means a single, empty byte string (while '0c' means 0 characters). When packing, arguments of types bytes and bytearray are accepted. For the 'Zf' and 'Zd' type codes, the packed representation uses the IEEE 754 binary32 and binary64 format for components of the complex number, regardless of the floating-point format used by the platform.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert python bytes to bit string
5 Best Ways to Convert Python Bytes to Bit String - Be on the Right Side of Change
February 23, 2024 - The code utilizes the struct.unpack ... f-string format specifier :08b. binascii.hexlify() converts bytes into a hex representation, which can be further processed into a binary string....
🌐
Github
he-arc.github.io › livre-python › binary › index.html
bytes / bytearray / memoryview, struct — Documentation Bibliothèques Python 1.0.0
# vérifiez avec sys.byteorder. single_byte = i.to_bytes(1, byteorder='big', signed=True) print(single_byte) # Crée un bytes avec une liste de int (0-255). # sortie: b'\xff\xfe\xfd\xfc. bytes_from_list = bytes([255, 254, 253, 252]) # Print out binary string (e.g.