Method # 1: Using join () + ord () + format ()
A combination of the above functions can be used to accomplish this particular task. The ord function converts a character to its ASCII equivalent, the format converts it to binary, and join is used to concatenate each converted character into a string.
|
Output:
The origi nal string is: Python.Engineering The string after binary conversion: 10001111100101110010111010111110011001101101111111001010001111100101110010111010111110011
Method # 2: Using join () + bytearray () + format () / strong>
This method is almost the same as the above function. The difference here is that instead of converting the character to its ASCII using the ord function, the conversion to the string is performed immediately by the bytearray function.
|
The original string is: Python.Engineering The string after binary conversion: 1000111110010111001011101011111001111001101101111111001010001111100101110010111010111110011