How do you convert string to binary in python?

In this post,we are going to learn how to convert string to binary in Python with examples using built in function join() ,ord() ,format() and bytearray(),map(),bin() and Math module.

1. Using ord(),format() to convert string to binary

  • 1. Using ord(),format() to convert string to binary
  • 2.join() and bytearray() to convert string to binary in Python
  • 3. Map() to convert string to binary in Python
  • 4. Math Module,ord(),bin() to convert string to binary
  • Summary


In this example, we are using three functions join(), ord(), format() to convert string to its binary representation and for loop to iterate over each character of the string, format() function to insert a value between placeholder finally using join() function to concatenate the element by space delimiter.

  • ord() : This function returns the number representing the unicode of given character. It takes a string as an argument and returns its unicode
  • format() : The format() function format the given value and insert the value between the placeholder.
  • Join() : The join() method concatenate the elements by delimiter.

Python Program to Convert string to binary in Python

def str_to_binary(Str):

 binaryStr = ' '.join(format(ord(item), 'b') for item in Str)
 print(binaryStr)

Str = "Good Morning"

str_to_binary(Str)


Output

1000111 1101111 1101111 1100100 100000 1001101 1101111 1110010 1101110 1101001 1101110 1100111

2.join() and bytearray() to convert string to binary in Python


In this example, we are using the bytearray() function that returns an array of bytes. In our example it return array of bytes for a given string “str”.We are iterating over an array of bytes to get binary by using format() inbuilt function by passing two parameters

  • val: The number that we want to convert into binary string
  • Format : It convert the passed value to given format.

The join() function concatenate the element by space delimiter. Let us understand with an example.

Python Program to convert string to binary in Python

In this example, we have defined a custom method str_binary() that takes a string as an argument and returns binary representation.

def str_binary(Str):

 binaryStr = ' '.join(format(i, '08b') for i in bytearray(Str, encoding ='utf-8'))
 print(binaryStr)

Str = "Good Morning"

str_binary(Str)

Output

01000111 01101111 01101111 01100100 00100000 01001101 01101111 01110010 01101110 01101001 01101110 01100111

3. Map() to convert string to binary in Python


In this example, we are using the bytearray that returns an array of the byte in our example its returns an array of bytes for a given string “str”, using map() along with bin() to get the binary of given string.

Python Program to convert string to binary in Python

def str_binary(Str):

 binaryStr = ' '.join(map(bin, bytearray(Str, encoding ='utf-8')))
 print(bytearray(Str, encoding ='utf-8'))
 print(binaryStr)

Str = "Good Morning"

str_binary(Str)

Output

0b1000111 0b1101111 0b1101111 0b1100100 0b100000 0b1001101 0b1101111 0b1110010 0b1101110 0b1101001 0b1101110 0b1100111

4. Math Module,ord(),bin() to convert string to binary


In this example we have defined a custom function name “Strto_Binary” first, we are using ord() to get numbers that represent Unicode of given characters in a string and append them to a list.

In the second step, we are using bin() to get the binary value of each character and [2:] to remove the prefix “0b” and appending them to result in the list(mod_list) and return the result list. We are calling the custom function “Strto_Binary” bypassing the string that we want to convert into binary.

Python Program to convert string to binary

def Strto_Binary(str):
  list,mod_list=[],[]
  for item in Str:
    list.append(ord(item))
  for item in list:
    mod_list.append(int(bin(item)[2:]))
  return mod_list
 
print(Strto_Binary("Good Morning"))

Output

[1000111, 1101111, 1101111, 1100100, 100000, 1001101, 1101111, 1110010, 1101110, 1101001, 1101110, 1100111]

Summary

In this post, we have learned how to convert string to binary in Python with code example by using built in function join() ,ord() ,format() and bytearray(),map(),bin() and Math module.

How do you convert data to binary in Python?

In Python, you can simply use the bin() function to convert from a decimal value to its corresponding binary value. And similarly, the int() function to convert a binary to its decimal value. The int() function takes as second argument the base of the number to be converted, which is 2 in case of binary numbers.

How do you convert string to decimal in Python?

Use float() to convert a string to a decimal.
pi = "3.14159".
decimal = float(pi).
print(decimal).

Is there a binary datatype in Python?

Python has the following data types built-in by default. ... 1. Built-in Data Types in Python..