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 › convert-binary-to-string-using-python
Convert binary to string using Python - GeeksforGeeks
July 12, 2025 - This method breaks the binary string into chunks of 8 bits and converts each chunk into a character using int() and chr() functions. It’s a clean and readable way to convert binary to text and works well for small to medium-length strings.
Discussions

binary data to string
I think you need to be a little more specific. More on reddit.com
🌐 r/learnpython
19
0
December 17, 2024
how do i convert string to binary string and the reverse too?
If you simply want to see the string output in binary, I just do: print(bin(yournumberhere)) More on reddit.com
🌐 r/learnpython
3
1
October 27, 2022
python - How to convert string to binary? - Stack Overflow
I am in need of a way to get the binary representation of a string in python. More on stackoverflow.com
🌐 stackoverflow.com
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
October 27, 2021
🌐
Python
docs.python.org › 3 › builtins › stdtypes.html
Built-in Types — Python 3.14.7 documentation
Otherwise, the bytes object underlying the buffer object is obtained before calling bytes.decode(). See Binary Sequence Types — bytes, bytearray, memoryview and Buffer Protocol for information on buffer objects. Passing a bytes object to str() without the encoding or errors arguments falls under the first case of returning the informal string representation (see also the -b command-line option to Python).
🌐
Python Morsels
pythonmorsels.com › representing-binary-data-with-bytes
Representing binary data with bytes - Python Morsels
December 27, 2021 - If we open up a file with the mode of rb, we're opening that file not in the default read-text mode, but instead in read-binary mode. >>> with open("avatar.jpg", mode="rb") as jpg_file: ... jpg_data = jpg_file.read() ... So when we read from that file, the data that we get out of it will not be a string, it'll be a bytes object. ... In fact in this case where we're opening up a jpg file, we get a bytes object with a lot of bytes in it, because it takes a lot of bytes to represent an image: ... If you end up with a bytes object in Python, and you know that the object represents text, you can turn it into a string by calling its decode method:
🌐
Reddit
reddit.com › r/learnpython › binary data to string
r/learnpython on Reddit: binary data to string
December 17, 2024 -

Hi how can i convert a binary data (read from any file) to a string which contains the exact same binary data so that i can manipulate it

Find elsewhere
🌐
AskPython
askpython.com › python › built-in-methods › convert-binary-string-to-normal-string
4 ways to Convert a Binary String to a Normal String - AskPython
April 23, 2023 - Related: Read this article to know more about the conversion of integers to binary. The bin() function is a built-in Python function used to obtain the binary representation of an integer.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-convert-string-to-binary
Convert String to Binary - Python - GeeksforGeeks
July 12, 2025 - Explanation: converts each character in txt to its 8-bit binary form using ord() and format() then joins them into one binary string using join().
Top answer
1 of 10
174

Something like this?

>>> st = "hello world"
>>> ' '.join(format(ord(x), 'b') for x in st)
'1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100'

#using `bytearray`
>>> ' '.join(format(x, 'b') for x in bytearray(st, 'utf-8'))
'1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100'
2 of 10
133

If by binary you mean bytes type, you can just use encode method of the string object that encodes your string as a bytes object using the passed encoding type. You just need to make sure you pass a proper encoding to encode function.

In [9]: "hello world".encode('ascii')                                                                                                                                                                       
Out[9]: b'hello world'

In [10]: byte_obj = "hello world".encode('ascii')                                                                                                                                                           

In [11]: byte_obj                                                                                                                                                                                           
Out[11]: b'hello world'

In [12]: byte_obj[0]                                                                                                                                                                                        
Out[12]: 104

Otherwise, if you want them in form of zeros and ones --binary representation-- as a more pythonic way you can first convert your string to byte array then use bin function within map :

>>> st = "hello world"
>>> map(bin,bytearray(st))
['0b1101000', '0b1100101', '0b1101100', '0b1101100', '0b1101111', '0b100000', '0b1110111', '0b1101111', '0b1110010', '0b1101100', '0b1100100']
 

Or you can join it:

>>> ' '.join(map(bin,bytearray(st)))
'0b1101000 0b1100101 0b1101100 0b1101100 0b1101111 0b100000 0b1110111 0b1101111 0b1110010 0b1101100 0b1100100'

Note that in python3 you need to specify an encoding for bytearray function :

>>> ' '.join(map(bin,bytearray(st,'utf8')))
'0b1101000 0b1100101 0b1101100 0b1101100 0b1101111 0b100000 0b1110111 0b1101111 0b1110010 0b1101100 0b1100100'

You can also use binascii module in python 2:

>>> import binascii
>>> bin(int(binascii.hexlify(st),16))
'0b110100001100101011011000110110001101111001000000111011101101111011100100110110001100100'

hexlify return the hexadecimal representation of the binary data then you can convert to int by specifying 16 as its base then convert it to binary with bin.

🌐
Ars OpenForum
arstechnica.com › forums › operating systems & software › programmer's symposium
Python question: string of binary to an actual binary number | Ars OpenForum
January 7, 2014 - Oddly (considering Python's "include all the things!" mentality), there actually isn't a succinct inverse to int(x, base) which will take an int and output a string of an arbitrary base (so, if for some strange reason you're trying to do base 5 math in python, you'll have to do a bit more work to convert into a base 5 string).
🌐
Educative
educative.io › answers › how-to-convert-string-to-binary-in-python
How to convert string to binary in Python
Strings are an array of Unicode code characters. Binary is a base-2 number system consisting of 0’s and 1’s that computers understand.
🌐
AskPython
askpython.com › python › built-in-methods › integer-binary-string-python
Integer to Binary String in Python - AskPython
February 28, 2023 - We can use built-in functions like bin() and format() to convert integers to binary strings. We can use string formatting methods like f-string ad simple string formatting for this conversion.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-string-to-binary-in-python
How to convert string to binary in Python?
March 24, 2026 - A binary number is a number expressed in the base-2 numerical system, which uses only two symbols - 0 and 1. Since computers store data as binary, converting strings into binary helps to understand how data is stored or transmitted. Python provides several methods to achieve this.
🌐
Quora
quora.com › How-do-you-convert-a-string-to-binary-in-Python
How to convert a string to binary in Python - Quora
Answer (1 of 2): You can convert a string to binary by converting each character to its Unicode representation, then converting it to binary. Let us take an example: [code]>>> s = "hello" [/code]Understanding bin(), ord(): * bin() is an in-built function in Python that takes in integer x and ...
🌐
Vultr Docs
docs.vultr.com › python › built-in › bin()
Python bin() - Convert Integer to Binary
September 27, 2024 - Whether handling positive or negative integers, the function provides clear, two's-complement formatted binary strings accurately and efficiently. Use bin() in a wide array of applications, from simple data representation transformations to more complex binary data processing tasks.
🌐
DataCamp
datacamp.com › tutorial › python-data-type-conversion
Python Data Type Conversion: A Guide With Examples | DataCamp
February 16, 2025 - Python has many data types. You must have already seen and worked with some of them. You have integers and float to deal with numerical values, boolean (bool) to deal with true/false values and strings to work with alphanumeric characters. You can use lists, tuples, dictionaries, and sets, which are data structures where you can store a collection of values.
🌐
Sololearn
sololearn.com › en › Discuss › 3006608 › convert-binary-to-string
Convert binary to string | Sololearn: Learn to code for FREE!
I wanna decode this binary to string : 01000111011001010110010101101011011100110110011001101111011100100100011101100101011001010110101101110011 ... You got it from GeeksforGeeks right? Let i = 0 Let result = "" #empty string While <i> less than length of string Slice 8 characters off the string, From offset <i> to <i> + 8 Convert that chunk into an integer using int function Specify 2 (binary) as conversion base Convert the integer to a character using chr function Push the character to <result> Increment <i> by 8 Print <result>
🌐
RapidTables
rapidtables.com › convert › number › binary-to-ascii.html
Binary to Text Translator
** UTF-8 text encoding uses a variable number of bytes for each character. This requires a delimiter between each binary number.
🌐
Learning Python
learning-python.com › strings30.html
Python 3.X Strings Tutorial by Mark Lutz
If you deal with binary data—for example, in the form of image or audio files, network transfers, or packed data processed with the struct module—you will need to understand 3.0's new bytes object and its different and sharper distinction between text and binary data and files. If you fall into neither of the prior two categories, you can generally use strings in 3.0 much as you would in 2.6: with the general str string type, text files, and all the familiar string operations.