Python | Convert String to Binary
Last Updated :
03 May, 2023
Data conversion have always been widely used utility and one among them can be conversion of a string to it’s binary equivalent. Let’s discuss certain ways in which this can be done.
Method #1 : Using join() + ord() + format() The combination of above functions can be used to perform this particular task. 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
test_str = "GeeksforGeeks"
print ("The original string is : " + str (test_str))
res = ' '.join(format(ord(i), ' 08b ') for i in test_str)
print ("The string after binary conversion : " + str (res))
|
Output :
The original string is : GeeksforGeeks
The string after binary conversion : 01000111011001010110010101101011011100110110011001101111011100100100011101100101011001010110101101110011
Time Complexity: O(N) where N is the lenght of the input string.
Auxiliary Space: O(N)
Method #2 : Using join() + bytearray() + format() This method is almost similar to the above function. The difference here is that rather than converting the character to it’s ASCII using ord function, the conversion at once of string is done by bytearray function.
Python3
test_str = "GeeksforGeeks"
print ("The original string is : " + str (test_str))
res = ' '.join(format(i, ' 08b ') for i in bytearray(test_str, encoding =' utf - 8 '))
print ("The string after binary conversion : " + str (res))
|
Output :
The original string is : GeeksforGeeks
The string after binary conversion : 01000111011001010110010101101011011100110110011001101111011100100100011101100101011001010110101101110011
Method #3 : Using join() + bin() + zfill()
we define a function str_to_binary(string) that takes in a string as its input.
We then create an empty list called binary_list, which will be used to store the binary conversions of each character in the input string.
We then use a for loop to iterate through each character in the input string. For each character, we use the ord() function to convert it to its ASCII equivalent, and then use the bin() function to convert that integer to a binary string. The bin() function returns a string with a ‘0b’ prefix, so we use list slicing to remove that prefix.
After that we use the zfill(8) method to pad the binary conversion with leading zeroes until it reaches a length of 8. The zfill() method pads the string on the left with a specified character (in this case, ‘0’) until it reaches the desired length.
Then we append the binary conversion to the binary_list and after that we join all the binary values in the list and return as a single string by using join() method.
Finally, we test the function with an example input of the string “GeeksforGeeks”. The function returns the binary string representation of the input string.
Python3
def str_to_binary(string):
binary_list = []
for char in string:
binary_list.append( bin ( ord (char))[ 2 :].zfill( 8 ))
return ''.join(binary_list)
test_str = "GeeksforGeeks"
print (str_to_binary(test_str))
|
Output
01000111011001010110010101101011011100110110011001101111011100100100011101100101011001010110101101110011
Time complexity: O(n), where n is the length of the input string.
Auxiliary Space: O(n), as we store each binary conversion in a list.
Method 4 : Using the binascii module.
Step-by-step approach:
- Import the binascii module.
- Initialize the string you want to convert to binary.
- Use the binascii.hexlify() function to convert the string to hexadecimal format.
- Use the binascii.unhexlify() function to convert the hexadecimal string to binary format.
- Decode the binary format using the .decode() method and store the result in a variable.
- Print the original string and the binary format.
Python3
import binascii
test_str = "GeeksforGeeks"
hex_str = binascii.hexlify(test_str.encode())
bin_str = bin ( int (hex_str, 16 ))[ 2 :].zfill( 8 * (( len (hex_str) + 1 ) / / 2 ))
res = bin_str
print ( "The original string is: " + str (test_str))
print ( "The string after binary conversion: " + str (res))
|
Output
The original string is: GeeksforGeeks
The string after binary conversion: 01000111011001010110010101101011011100110110011001101111011100100100011101100101011001010110101101110011
Time complexity: O(n)
Auxiliary space: O(n).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...