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
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 - 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().
Discussions

Convert int to binary string in Python - Stack Overflow
How do I convert an integer into a binary string in Python? 37 โ†’ '100101' More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to convert 'binary string' to normal string in Python3? - Stack Overflow
For example, I have a string like this(return value of subprocess.check_output): >>> b'a string' b'a string' Whatever I did to it, it is always printed with the annoying b' before the str... 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
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
๐ŸŒ
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.
๐ŸŒ
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 - In Python, a string can be converted to its binary representation using the format(), map(), bytearray(), bin(), and ASCII methods.
๐ŸŒ
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 ...
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.
๐ŸŒ
DEV Community
dev.to โ€บ 221910304057vivek โ€บ how-to-convert-string-to-binary-in-python-2igb
How to convert string to binary in python - DEV Community
May 7, 2021 - To convert a string to binary we first append( join ) the string's individual ASCII values to a list l by using the function ord(_string).
๐ŸŒ
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.
๐ŸŒ
Medium
medium.com โ€บ @roscoe.kerby โ€บ understanding-string-to-binary-conversion-in-python-with-a-simple-example-ceddafdee676
Understanding String-to-Binary Conversion in Python with a simple example | by Roscoe Kerby [ROSCODE] | Medium
August 5, 2024 - String-to-Binary conversion involves representing each character in a string as a sequence of binary digits (bits). This process assigns a unique binary code to each character, enabling computers to store and manipulate textual data efficiently.
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
Sling Academy
slingacademy.com โ€บ article โ€บ python-convert-a-string-to-binary-codes
Python: 3 Ways to Convert a String to Binary Codes - Sling Academy
June 1, 2023 - Concatenate the binary codes to form the final binary string. Thanks to Pythonโ€™s concise syntax, our code is very succinct:
๐ŸŒ
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
For full Unicode support, encode the string to bytes first: ... The snake emoji (๐Ÿ) requires 4 bytes in UTF-8 encoding, which is why you see four 8-bit sequences for a single character. The first three groups represent H, i, and a space, each using a single byte. binary_input = "01001000 01101001 00100000 11110000 10011111 10010000 10001101"
๐ŸŒ
Python Guides
pythonguides.com โ€บ convert-string-of-1s-and-0s-to-binary-in-python
Convert String Of 1s And 0s To Binary In Python
May 16, 2025 - Read How to Print Strings and Variables in Python? The most simple and commonly used approach is to use Pythonโ€™s built-in int() function with a base parameter of 2. # Converting a binary string to an integer binary_string = "01101101" binary_value = int(binary_string, 2) print(f"Binary string: {binary_string}") print(f"Integer value: {binary_value}") print(f"Binary representation: {bin(binary_value)}")
๐ŸŒ
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.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ builtins โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.7 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 ...