PYTHON STRING TO BYTES & PYTHON BYTES TO STRING

In this article, we are going to discuss the conversion of  Python String to Bytes and Python Bytes to String. Encoding and decoding of the strings in python with suitable examples. Since python3 has released …

PYTHON STRING TO BYTES & PYTHON BYTES TO STRING

In this article, we are going to discuss the conversion of  Python String to Bytes and Python Bytes to String. Encoding and decoding of the strings in python with suitable examples.

Since python3 has released the conversion of python string to byte and python byte to string and has been very common because of machine learning algorithms and methods.

Now the question arises:

What is the need to convert a string into byte and byte into string in python?

The answer is that strings are human-readable, as humans can not read and understand the data or information in a way they can understand the string, same goes for machines as we all know machines store information in the form of bits and bytes. So, we convert the string into bytes for readability and understanding of the machine.

Python String into byte:

The conversion from string to byte will allow the machine to store the data into the disk directly.

Python string to bytes can be done in two ways:

  • By using byte( ) method
  • By using encode( ) method.

The process in which we convert a string into a byte is known as encoding. And the process in which we convert a byte into a string is known as decoding.

Byte( ) method:

Byte( ) method is used to convert python string into a byte, byte ( ) is a built-in function in python.

Syntax:

bytes(‘string’ , encoding= ‘utf-8’ )

Let’s see how it works with the help of an example:

Example:

#program to convert python string to byte
str1 = 'Hello Drastic Family';
byte1 = bytes(str1, encoding='utf-8')
print(byte1)
print(type(byte1))

In this example, 

First, we declare the string that has to be converted into a byte.

Then, to use the byte() method to change the string into a byte.

In the end, used the print function to print the output.

Output:

b'Hello Drastic Family'
<class 'bytes'>

Here, the output “ b’ ” shows the byte.

We can also use the byte ( ) method in a different way:

Let’s check its example:

Example:

Python String into bytes ASCII

string = "Hello Drastic Family"
# string with encoding 'utf-8'

arr = bytes(string, 'utf-8')
arr2 = bytes(string, 'ascii')
print(arr,'\n')

# actual bytes in the string

for byte in arr:
   print(byte, end=' ')
print("\n")
for byte in arr2:
    print(byte, end=' ')

Output:

b'hello Drastic family' 

104 101 108 108 111 32 68 114 97 115 116 105 99 32 102 97 109 105 108 121 

>

Encode( ) method:

Encode( ) method is the most flexible method to convert python string into a byte.

Syntax:

‘String’ .encode( ‘ utf-8’ )

Let’s see how it works with the help of an example:

Example:

# string to byte
str = 'Drastic Code';
byte = str.encode( 'utf-8' )
print(byte)
print(type(byte))

In this example, 

First, we declared the string that has to be converted into a byte.

Then, we used the encode ( ) method.

In the end, we used the print function to print the output.

Output:

b'Drastic Code'
<class 'bytes'>

Here, the output “ b’ ” shows the byte.

Python byte into String:

Conversion of python byte into a string is also a common practice like conversion of python string into a byte.

The process of converting a byte into a string is called a decoding function.

There are three methods that you can use to convert python byte to string:

  1. Decode ( ) method.
  2. Codecs.decode ( ) method.
  3. Str( ) function.

Decode( ) method:

Decode method is a build-in method in python to change byte into a string.

Syntax:

String.decode( ‘ utf-8 ’ )

Let’s take an example of the decode ( )  method to understand it more clearly:

Example:

#Simple decoding byte to string
byte = b'Drastic Family'.decode()
print(byte)

#utf-8 to string
decoded = b'Drastic Family'.decode('utf-8')
print(decoded)

In this example, we have performed the simple decoding byte to string.

In the byte function, we performed the decode method’s syntax.

To print the decoded output.

After that, we used the utf-8 method to decode the byte into a string.

Inside the decoded function we used the decode method,

And in the end, printed the decoded output.

Output:

Drastic Family
Drastic Family
>

Codecs.decode ( ) method:

Codecs.decode ( ) is a python method is used to decode the string from a byte.

Syntax:

Codecs.decode(b ‘string)

Let’s check its example:

Example:

  # import required module
import codecs
  
data = b'Hello Drastic Family'
 
# converting
output = codecs.decode(data)
  
# display output
print(output)

In this example of codecs.decode:

First, we used the import function to import codecs.

Then, used the data keyword to show the data that has to be converted into a string that is already in byte form.

For converting the data from byte to string in python we used codecs.decode method.

In the end, displayed the output by using the print function.

Output:

Hello Drastic Family
>

Conclusion:

In this article, we discussed python string to byte and python byte to string two vice-versa topics and discussed how we can achieve this conversion. For the conversion of python string to byte we used two methods: Byte( ) method and Encode method. And, for the conversion of the python byte to String, we also used two methods: Decode ( ) method and codecs. decode( ) method.

Suggested Read: Remove Brackets from list Python

Leave a Comment