String to Binary in PythonIn 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-
|
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263424.html