You need to pass in a bytes object, rather than a str. The typical way to go from str (a unicode string in Python 3) to bytes is to use the .encode() method on the string and specify the encoding you wish to use.

my_bytes = my_string.encode('utf-8')
Answer from Amber on Stack Overflow
🌐
Python
docs.python.org › 3 › library › binascii.html
binascii — Convert between binary and ASCII
Convert binary data to a line(s) of ASCII characters in quoted-printable encoding. The return value is the converted line(s). If the optional argument quotetabs is present and true, all tabs and spaces will be encoded.
Discussions

python - How to convert string to binary? - Stack Overflow
hexlify return the hexadecimal ... it to binary with bin. ... Not only this is more pythonic, but this is "more" correct for multi-byte non-ASCII strings. 2017-03-25T20:23:31.033Z+00:00 ... Just to note that (at least for the current version 3.7.4): (1) bytearray expects an encoding (not just ... More on stackoverflow.com
🌐 stackoverflow.com
python - How to convert 'binary string' to normal string in Python3? - Stack Overflow
We often use 'strings' to contain binary data for instance making DNS requests etc. 2016-09-23T11:55:42.38Z+00:00 ... I suggest to add the following to complete the answer. Most times we need to decode bytes from our operating system, such as console output, the most pythonic way I found to do it is to import locale and then os_encoding ... More on stackoverflow.com
🌐 stackoverflow.com
git - Python Encoding/Decoding with Binary Strings - Stack Overflow
Referencing: https://stackoverflow.com/questions/6224052/what-is-the-difference-between-a-string-and-a-byte-string#:~:text=In Python, a byte string,is a sequence of characters. ... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
AskPython
askpython.com › python › examples › binary-to-utf8-conversion
How to Convert Binary Data to UTF-8 in Python - AskPython
April 10, 2025 - Another technique that works for unknown binary data is to base64 encode it first. Base64 produces UTF-8 compatible text output from any binary input. import base64 data = b"\x12\xab" # Base64 encode b64_data = base64.b64encode(data) # Now decode base64 text from UTF-8 to string text = b64_data.decode("utf-8") print(text) # Output: Eqs=
🌐
GeeksforGeeks
geeksforgeeks.org › python › working-with-binary-data-in-python
Working with Binary Data in Python - GeeksforGeeks
July 15, 2025 - As seen above it is possible to encode or decode strings and binary strings using the encode() or decode() function. We need the encoding because in some encodings it is not possible to to decode the strings.
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.

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-convert-string-to-binary
Convert String to Binary - Python - GeeksforGeeks
July 12, 2025 - Python · import binascii txt = ... turns those bytes into a hexadecimal string · bin(int(..., 16))[2:] converts the hex string to binary, removing the '0b' prefix ·...
Find elsewhere
🌐
Packtpub
subscription.packtpub.com › book › data › 9781804611302 › 2 › ch02lvl1sec21 › performing-binary-encoding
Chapter 2: Encoding Categorical Variables | Python Feature Engineering Cookbook
For example, integer 1 can be represented as sequence 10, integer 2 as 01, integer 3 as 11, and integer 0 as 00. The digits in the two positions of the binary string become the columns, which are the encoded representations of the original ...
🌐
Delft Stack
delftstack.com › home › howto › python › string to binary python
How to Convert a String to Binary in Python | Delft Stack
February 2, 2024 - But this method will use an ASCII encoding scheme instead of utf-8. For basic alphanumeric strings, the variations between UTF-8 and ASCII encoding are not noticeable. But they will become significant if we are processing text that incorporates characters that are not part of the ASCII character collection. ... st = "Python" a_bytes = bytes(st, "ascii") binary_converted = " ".join(["{0:b}".format(x) for x in a_bytes]) print("The Binary Represntation is:", binary_converted)
🌐
GeeksforGeeks
geeksforgeeks.org › convert-binary-to-string-using-python
Convert binary to string using Python - GeeksforGeeks
April 12, 2025 - Finally, join() combines all characters into a single string. In this approach, the whole binary string is first turned into an integer. Then it’s converted to bytes and finally decoded into a string.
🌐
Learning Python
learning-python.com › strings30.html
Python 3.X Strings Tutorial by Mark Lutz
At a more concrete level, the Python language provides multiple string data types to represent content in your script: both textual data—integer code-point values of decoded Unicode characters in memory, as well as binary data—raw byte values, including text that is in encoded form.
🌐
Tutorial Reference
tutorialreference.com › python › examples › faq › python-how-to-convert-string-to-binary
How to Convert a String to Binary in Python | Tutorial Reference
The ord() approach gives you the raw code point in binary, while UTF-8 encoding gives you the actual byte representation used for storage and transmission. For most real-world applications, the UTF-8 approach is what you need. For production code, wrap these operations in reusable functions with proper error handling: def string_to_binary(text: str, encoding: str = 'utf-8') -> str:
🌐
Python Module of the Week
pymotw.com › 2 › base64
base64 – Encode binary data into ASCII characters - Python Module of the Week
$ python base64_urlsafe.py Original : '\xfb\xef' Standard encoding: ++8= URL-safe encoding: --8= Original : '\xff\xff' Standard encoding: //8= URL-safe encoding: __8= Besides base 64, the module provides functions for working with base 32 and base 16 (hex) encoded data. import base64 original_string = 'This is the data, in the clear.' print 'Original:', original_string encoded_string = base64.b32encode(original_string) print 'Encoded :', encoded_string decoded_string = base64.b32decode(encoded_string) print 'Decoded :', decoded_string
🌐
Stack Overflow
stackoverflow.com › questions › 75640460 › python-encoding-decoding-with-binary-strings
git - Python Encoding/Decoding with Binary Strings - Stack Overflow
If you open a file with 'r', it gets decoded through UTF-8 and becomes Python Unicode strings. If you open a file with 'rb', it is not decoded. A 4,000-byte file will become a 4,000-byte string. ... These aren't "binary strings" or "binary data". We open the file in "binary mode", as opposed to "text mode", which was inherited from Windows. A file opened in "binary mode" produces "byte strings" that are not Unicode-encoded.
🌐
freeCodeCamp
freecodecamp.org › news › python-bytes-to-string-how-to-convert-a-bytestring
Python Bytes to String – How to Convert a Bytestring
April 10, 2023 - We specify the encoding format as utf-8 using the encoding parameter. Finally, we print the resulting string to the console. We can also use the bytes() constructor, a built-in Python function used to create a new bytes object.
🌐
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 - A variable called ascs is used to store the conversion of the binary data to normal string with the help of tobytes and decode. The encoding schema is specified to be ascii.
🌐
Theproductguy
theproductguy.in › home › blogs › binary encoding in programming
Binary Encoding in Programming | theproductguy.in
February 22, 2026 - How computers encode text as binary — ASCII, UTF-8, and Unicode in Python, JavaScript, and Java.
🌐
Python Morsels
pythonmorsels.com › representing-binary-data-with-bytes
Representing binary data with bytes - Python Morsels
December 27, 2021 - 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: >>> data = b"bytes! \xe2\x9c\xa8" >>> data.decode() 'bytes! ✨' The decode method (without any arguments passed to it) uses a default character encoding of utf-8.