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'
Answer from Ashwini Chaudhary on Stack Overflow
🌐
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.

Discussions

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
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 to convert bytes to string in python?
Try this: line = ser.readline().strip() values = line.decode('ascii').split(',') a, b, c = [int(s) for s in values] The call to .strip() removes the trailing newline. The call .decode('ascii') converts the raw bytes to a string. .split(',') splits the string on commas. Finally the call [int(s) for s in value] is called a list comprehension, and produces a list of integers. More on reddit.com
🌐 r/Python
3
1
February 8, 2016
Converting integer to 16-bit hexadecimal in Big-endain form

Check out hex() and, more importantly struct:

http://docs.python.org/2/library/struct.html

This lets you convert a decimal into binary data. I've used it to read binary files, so it might be what you are looking for. I'm weak on theory so I tend to just bang on stuff until I get it right, but I would start with:

>>> import struct
>>> struct.pack('>h', -200)
b'\xff8'

And see what the machine makes of it, and adjust accordingly.

More on reddit.com
🌐 r/learnpython
12
4
March 6, 2014
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-binary-to-string-using-python
Convert binary to string using Python - GeeksforGeeks
July 12, 2025 - We are given a binary string and need to convert it into a readable text string. The goal is to interpret the binary data, where each group of 8 bits represents a character and decode it into its corresponding text.
🌐
Educative
educative.io › answers › how-to-convert-string-to-binary-in-python
How to convert string to binary in Python
Next, we can append these binary values to a list (m) so that m now consists of the binary numbers from the strings given and can be returned or printed. ... In order to pass a list of binary numbers to the function, we first convert the individual numbers to ASCII value by binary to decimal ...
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 - In Python, whenever we try to represent a binary string, there is always a prefix called the 0b prefix used to represent that it is a binary number. Most of the time, when we try to convert the binary string to a character string or vice versa, we try to eliminate this prefix.
🌐
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.
🌐
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.
🌐
Python Morsels
pythonmorsels.com › representing-binary-data-with-bytes
Representing binary data with bytes - Python Morsels
August 3, 2026 - The decode method (without any arguments passed to it) uses a default character encoding of utf-8. Even if we know that the data we're working with uses that default character encoding of utf-8, it's considered a best practice to always specify the encoding of our bytes: >>> text = data.decode("utf-8") >>> text 'bytes! ✨' As the Zen of Python says, "explicit is always better than implicit". If for some reason you have a string you want to turn into bytes, you can call the encode method on that string to encode it into bytes:
🌐
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 - I have a string of 1s and 0s that represents a binary number (e.g. '00101101'). How would I convert that to an actual num? If I use int(), Python assumes decimal and creates a number that's much, much bigger (in the above example, it would return the # 101101 instead of 45). I tried the...
🌐
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

🌐
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 ...
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-string-to-binary-in-python
How to convert string to binary in Python?
March 24, 2026 - The second approach uses the Python bytearray() and bin() functions. The bytearray() function returns a new array of bytes, and the bin() function converts an integer to its binary equivalent as a string.
🌐
Tistory
7stocks.tistory.com › 47
Binary to String, String to Binary :: 개인 블로그
March 9, 2018 - 간단하다. Code snippets to show you how to convert string to bytes and vice versa.1. To convert a string to bytes.data = "" #string data = "".encode()#bytes data = b"" #bytes2. To convert bytes to a String.data = b"" #bytes data = b"".decode() #string data = str(b"") #stringP.S Tested with Python 3.4.3
🌐
Code Beautify
codebeautify.org › binary-string-converter
Binary to String Converter Online tool
This tool allows loading the Binary URL, which loads Binary and converts to String Numeral System.
🌐
Medium
medium.com › @python-javascript-php-html-css › transforming-byte-data-into-strings-in-python-3-c63dc99efbae
Converting Python 3 Byte Data into Strings
August 24, 2024 - The necessity of this conversion stems from Python’s distinct handling of byte sequences and text strings. Bytes, representing raw binary data, and strings, encapsulating human-readable text, serve different purposes in programming. Understanding how to seamlessly convert between these types is essential for implementing features such as reading files in a non-text format, receiving data over a network, and parsing binary protocols.
🌐
Python
docs.python.org › 3 › builtins › stdtypes.html
Built-in Types — Python 3.14.8 documentation
Two methods support conversion to and from hexadecimal strings. Since Python’s floats are stored internally as binary numbers, converting a float to or from a decimal string usually involves a small rounding error. In contrast, hexadecimal strings allow exact representation and specification ...
🌐
frhyme.code
frhyme.github.io › python-lib › binary_str_to_str
binary string을 string으로 바꾸기 : frhyme.code
July 3, 2018 - python에서 직접 bash command를 사용할 일이 있습니다. 저는 subprocess module을 사용해서 직접 커맨드를 치곤 합니다. 그런데, 이 때 결과물을 보면, string이 아니라 binary format으로 들어오는 것을 알 수 있어요.
🌐
Sololearn
sololearn.com › en › Discuss › 3006608 › convert-binary-to-string
Convert binary to string | Sololearn: Learn to code for FREE!
How can i convert binary to string in python? I wanna decode this binary to string : 01000111011001010110010101101011011100110110011001101111011100100100011101100101011