Decode it.

>>> b'a string'.decode('ascii')
'a string'

To get bytes from string, encode it.

>>> 'a string'.encode('ascii')
b'a string'
Answer from falsetru on Stack Overflow
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ reference โ€บ datamodel.html
3. Data model โ€” Python 3.14.3 documentation
These represent numbers in an unlimited range, subject to available (virtual) memory only. For the purpose of shift and mask operations, a binary representation is assumed, and negative numbers are represented in a variant of 2โ€™s complement which gives the illusion of an infinite string of sign ...
Discussions

String to binary
https://www.geeksforgeeks.org/python-convert-string-to-binary/ https://stackoverflow.com/questions/18815820/convert-string-to-binary-in-python https://www.tutorialspoint.com/How-to-convert-string-to-binary-in-Python Google is your friend !!! More on reddit.com
๐ŸŒ r/learnpython
4
1
May 6, 2020
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 to String/Text in Python - Stack Overflow
I have searched many times online and I have not been able to find a way to convert my binary string variable, X X = "1000100100010110001101000001101010110011001010100" into a UTF-8 string value. I More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
Top answer
1 of 10
172

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.

๐ŸŒ
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.
๐ŸŒ
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().
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ string to binary
r/learnpython on Reddit: String to binary
May 6, 2020 -

I want a function that takes for example 'Hello brother! ๐Ÿ˜Š'(a combination of any unicode symbols) as input and outputs the binary equivalent using utf-8 so here:

'01001000 01100101 01101100 01101100 01101111 00100000 01100010 01110010 01101111 01110100 01101000 01100101 01110010 00100001 00100000 11110000 10011111 10011000 10001010'

The output should also be a string.

Find elsewhere
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.3 documentation
February 25, 2026 - 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 ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_strings_modify.asp
Python - Modify Strings
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS ยท Python HOME Python Intro Python Get Started Python Syntax ... Python Variables Variable Names Assign Multiple Values Output Variables Global Variables Variable Exercises Code Challenge Python Data Types ... Python Strings Slicing Strings Modify Strings Concatenate Strings Format Strings Escape Characters String Methods String Exercises Code Challenge Python Booleans
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ representing-binary-data-with-bytes
Representing binary data with bytes - Python Morsels
December 27, 2021 - If we loop over a string in Python, we'll get back sub-strings representing each of the characters in that string: >>> text = "hello" >>> list(text) ['h', 'e', 'l', 'l', 'o'] What do you think we'll get if we loop over a bytes object? ... Since bytes objects represent binary data, when we loop over them we get back numbers (from 0 to 255) representing each of the bytes in that binary data:
๐ŸŒ
scikit-learn
scikit-learn.org โ€บ stable โ€บ modules โ€บ model_evaluation.html
3.4. Metrics and scoring: quantifying the quality of predictions โ€” scikit-learn 1.8.0 documentation
Note that the dict values can either be scorer functions or one of the predefined metric strings. As a callable that returns a dictionary of scores: >>> from sklearn.model_selection import cross_validate >>> from sklearn.metrics import confusion_matrix >>> # A sample toy binary classification dataset >>> X, y = datasets.make_classification(n_classes=2, random_state=0) >>> svm = LinearSVC(random_state=0) >>> def confusion_matrix_scorer(clf, X, y): ...
๐ŸŒ
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.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ sqlite3.html
sqlite3 โ€” DB-API 2.0 interface for SQLite databases
February 25, 2026 - Source code: Lib/sqlite3/ SQLite is a C library that provides a lightweight disk-based database that doesnโ€™t require a separate server process and allows accessing the database using a nonstandard ...
๐ŸŒ
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

๐ŸŒ
Hacker News
news.ycombinator.com โ€บ item
We rewrote our Rust WASM parser in TypeScript and it got faster | Hacker News
1 week ago - The port had been done in a weekend just to see if we could use Python in production. The C++ code had taken a few months to write. The port was pretty direct, function for function. It was even line for line where language and library differences didn't offer an easier way ยท A couple of us ...
๐ŸŒ
Reintech
reintech.io โ€บ blog โ€บ python-bin-method-tutorial
Python: Working with the bin() Method | Reintech media
January 20, 2026 - The bin() method in Python is a built-in function that allows you to convert an integer into its binary representation. This tutorial covers how to use this method and how it can be combined with other string methods.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ binascii.html
binascii โ€” Convert between binary and ASCII
Changed in version 3.3: ASCII-only unicode strings are now accepted by the a2b_* functions. The binascii module defines the following functions: ... Convert a single line of uuencoded data back to binary and return the binary data.
๐ŸŒ
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.
๐ŸŒ
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