String to Binary in PythonLast Updated : 17 Mar 2025 In this article, we will learn how we convert a string to its binary equivalent in Python. We know that strings are a sequence of strings and are denoted with inverted commas. Binary numbers are in the form 0s and 1s, the information is always encoded in the binary format since this is what the computer understands. The methods of converting a string to the binary that we will use here are using join(), ord(), format() and bytearray(). We should take of the corresponding ASCII values of the characters that are present in the string and convert them into binary. Let us have a look at the description of the functions that we have taken in our toolbox-
The following program shows how it can be done- Example - Output- The string that we have taken is Let's learn Python The string that we obtain binary conversion is 010011000110010101110100001001110111001100100000011011000110010101100001011100100110111000100000010100000111100101110100011010000110111101101110 Explanation - Let us understand what we have done in the above program-
In the next example, we will do the same thing by using bytearray(). Example - 2 Output- The string that we have taken is Let's learn Python The string that we obtain binary conversion is 010011000110010101110100001001110111001100100000011011000110010101100001011100100110111000100000010100000111100101110100011010000110111101101110 Example - Let us see how different the above approach was-
Next TopicWhat is script mode in Python |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India