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
🌐
Programiz
programiz.com › python-programming › methods › built-in › bin
Python bin() (With Examples)
Here, the prefix 0b in the output 0b101 represents that the result is a binary string. class Quantity: apple = 1 orange = 2 grapes = 2 def func(): return apple + orange + grapes print('The binary equivalent of quantity is:', bin(Quantity())) Output · TypeError: 'Quantity' object cannot be interpreted as an integer · Here, we have passed an object of class Quantity to the bin() method and got a TypeError. This is because we have used a non-integer class. Note: We can fix the TypeError above by using the Python __index__() method with a non-integer class.
🌐
w3resource
w3resource.com › python › built-in-function › bin.php
Python: bin() function - w3resource
April 16, 2026 - x = 10 print("Original number: ",x) print(x) print("Binary string:") print(bin(x)[2:].zfill(8)) x = -10 print("\nOriginal number: ",x) print(x) print("Binary string:") print(bin(x)[3:].zfill(8))
🌐
Real Python
realpython.com › ref › builtin-functions › bin
bin() | Python’s Built-in Functions – Real Python
The built-in bin() function converts an integer number into its binary representation, returning it as a string.
🌐
Reintech
reintech.io › blog › python-bin-method-tutorial
Python: Working with the bin() Method - Reintech
March 24, 2023 - The bin() function is a built-in Python utility that converts integers into their binary string representation.
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-binary-to-string-using-python
Convert binary to string using Python - GeeksforGeeks
July 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.
🌐
HowToDoInJava
howtodoinjava.com › home › python built-in functions › python bin() method
Python bin() - Converting number to binary string
October 1, 2022 - Python bin() method converts a given integer to it’s equivalent binary string, if not number, it has to provide __index__() method which must return integer
🌐
GeeksforGeeks
geeksforgeeks.org › bin-in-python
bin() in Python - GeeksforGeeks
November 15, 2023 - Python bin() function returns the binary string of a given integer. bin() function is used to convert integer to binary string.
Find elsewhere
🌐
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.

🌐
Educative
educative.io › answers › how-to-convert-string-to-binary-in-python
How to convert string to binary in Python
The string, as seen by the computer, is a binary number that is an ASCCI value( Decimal number) of the string converted to binary.
🌐
GeeksforGeeks
geeksforgeeks.org › python-convert-string-to-binary
Python | Convert String to Binary - GeeksforGeeks
May 3, 2023 - The ord function converts the character to it's ASCII equivalent, format converts this to binary number and join is used to join each converted character to form a string. ... # Python3 code to demonstrate working of # Converting String to binary ...
🌐
Mkyong
mkyong.com › home › python › python – how to convert int to a binary string?
Python – How to convert int to a binary string? | mkyong.com
June 2, 2020 - In Python, we can use bin() or format() to convert an integer into a binary string representation.
🌐
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.
🌐
alpharithms
alpharithms.com › home › tutorials › python bin(): binary values handled with ease
Python bin(): Binary Values Handled with Ease - αlphαrithms
June 16, 2022 - The Python bin() method converts an integer value to a string representation of its binary representation. Python’s bin() method can also be used to return the binary representation of any object that has implemented a valid __index__ method.
🌐
Scaler
scaler.com › home › topics › python bin() function
Python bin() Function - Scaler Topics
January 2, 2024 - bin() is an in-built function in Python that takes in integer x and returns the binary representation of x in a string format.
🌐
Vultr Docs
docs.vultr.com › python › built-in › bin()
Python bin() - Convert Integer to Binary
September 27, 2024 - ... Here, the binary representation ... be of consistent length, like byte-aligned data manipulation. The bin() function in Python offers a straightforward method to convert integers into their binary representations....
🌐
FavTutor
favtutor.com › blogs › binary-string-to-int-python
Convert Binary String to Int in Python (with code)
September 12, 2023 - We can divide the binary string into groups of 8 bits, use the int() function alongside the base argument set to 2 (binary) to convert each group into the corresponding integer, and then pass an integer argument to the chr() function to obtain the corresponding character. The original string can then be obtained by joining all the characters. The Python code to convert binary to string is as follows: