How to Convert Python string to byte array with Examples

In this python tutorial, you will learn about python string to byte array. Also, we will check:

  • Python string to byte array encoding
  • Python string to byte array UTF-8
  • Python string to byte array UTF-16
  • Python string to byte array hex
  • Python base64 string to byte array
  • Python binary string to byte array
  • Python JSON string to byte array
  • Python string to a byte array without b
  • Python hex string to byte array
  • Python string to byte array ASCII
  • Python 2.7 string to byte type

Python string to byte array

Now, we can see how to convert string to byte array in python.

  • In this example, I have taken string as “python guides” and to convert that string to byte, I have used new_string = bytes(string,”ascii”).
  • The bytearray() method returns a byte array object.

Example:

string = "python guides"
new_string = bytes(string,"ascii")
print(new_string)

To print the byte, I have used print(new_string). You can see in the output a converted string in the below screenshot.

Python string to byte array
Python string to byte array

Python string to byte array encoding

Here, we can see how to convert string to byte array by encoding in python.

In this example, I have taken a string as”python guides” and encoded it into a byte array by using new_string = string.encode(). The encode() method is used to encode the string.

Example:

string = "python guides"
new_string = string.encode()
print(new_string)

To print the encoded string, I have used print(new_string). You can refer to the below screenshot for the output.

Python string to byte array encoding
Python string to byte array encoding

Python string to byte array UTF-8

Now, we can see how to convert a string to byte array utf-8 in python

In this example, I have taken a string as “Python guides”. To convert the string into UTF-8, I have used newstring = bytes(string, ‘utf-8’). The bytearray() method returns the byte array object.

Example:

string = "Python guides."
newstring = bytes(string, 'utf-8')
print(newstring)

To print the converted string, I have used print(newstring). Below screenshot shows the output.

Python string to byte array UTF
Python string to byte array UTF

Python string to byte array UTF-16

Here, we can see how to convert string to byte array UTF-16 in python

  • In this example, I have taken a string as “Python guides”. To convert the string into UTF-16. I have used newstring = bytes(string, ‘utf-16’). The bytearray() method returns the byte array object.

Example:

string = "Python guides."
newstring = bytes(string, 'utf-16')
print(newstring)

To print the converted string, I have used print(newstring). You can see the utf-16 formatted string as the output in the below screenshot.

Python string to byte array UTF-16
Python string to byte array UTF-16

Python string to byte array hex

Let’s see how to convert string to byte array hex in python

  • In this example, I have taken a string as string = “AB CD EF” to convert string to byte array hex, I have used newstring = bytearray.fromhex(string).
  • The bytearray.fromhex() this method returns a required string containing hex numbers.

Example:

string = "AB CD EF"
newstring = bytearray.fromhex(string)
print(newstring)

To print the string to byte array hex, I have used print(newstring). You can refer to the below screenshot for the output.

Python string to byte array hex
Python string to byte array hex

Python base64 string to byte array

Here, we can see how to convert base64 string to byte array in python

  • In this example, I have imported a module called base64. This module is used to convert binary data to ASCII characters.
  • To decode the string, we have to use string = base64.b64decode(“Pythonguides”).

Example:

import base64
string = base64.b64decode("Pythonguides")
print(string)

To print the encoded string, we have to use print(string). You can refer to the below screenshot for the output.

Python base64 string to byte array
Python base64 string to byte array

Python binary string to byte array

Now, we can how to convert binary string to byte array in python

  • In this example, I have taken a binary string as string = “11000010110001001100011”. To convert the binary string to a byte array.
  • I have used new_string = bytearray(string, “ascii”). The bytearray() method returns the byte array object.

example:

string = "11000010110001001100011"
new_string = bytearray(string,"ascii")
print(new_string)

To print the converted string, I have used print(new_string). Below screenshot shows the output.

Python binary string to byte array
Python binary string to byte array

Python JSON string to byte array

Here, we can see how to convert JSON string to byte array in python

  • In this example, I have imported a module called JSON. The JSON module provides the API which is similar to pickle for converting in the memory.
  • The JSON string is similar to javascript objects. In JSON keys must be a string with a double-quote(” “).
  • json_string = ‘{“Name”: “Kushi”, “subjects”: [“english”, “maths”]}’ is the JSON string.
  • The bytearray() method is used to convert the JSON string to bytearray string, The bytearray() method returns the byte array objects.

Example:

import json
json_string = '{"Name": "Kushi", "subjects": ["english", "maths"]}'
new_string = bytearray(json_string,"ansi")
print(new_string)

To print the converted string, I have used print(new_string). In the below screenshot you can see the output as json string converted into bytearray.

Python JSON string to byte array
Python JSON string to byte array

Python string to a byte array without b

Here, we can how to convert string to a byte array without b in python

In this example, I have taken a string as b’ python guides’ which includes a byte array, then the string is decoded to print the byte array without b. To decode the string, I have used newstring = string.decode().

Example:

string = b'python guides'
print(string)
newstring = string.decode()
print(newstring)

To print the string without b I have used print(newstring). You can refer to the below screenshot for the output.

Python string to a byte array without b
Python string to a byte array without b

Python hex string to byte array

Now, we can see how to convert hex string to byte array in python

In this example, I have taken a hex_string = “1A” and to convert that string to bytearray I have used newstring = bytearray.fromhex(hexa_string). The bytearray.fromhex() method returns a required string containing hex numbers.

Example:

hexa_string = "1A"
newstring = bytearray.fromhex(hexa_string)
print(newstring)

To print the hex string converted to a byte array, I have used print(newstring). You can refer to the below screenshot for the output.

Python string to byte array ASCII

Here, we can see how to convert string to bytearray ASCII in python

In this example, I have taken a string as (‘python guides’), To convert the string into bytearray ASCII, I have used string = bytes(str(‘Python guides’).encode(“ascii”)).

Example:

string = bytes(str('Python guides').encode("ascii"))
print(string)

To print the converted string, I have used print(string). You can refer to the below screenshot for the output

Python string to byte array ASCII
Python string to byte array ASCII

Python 2.7 string to byte type

Here, we can see that a given string is of which type in python

In this example, I have taken three arguments a = “hi”, b=bytes(0), c=25 and to get the type of given string, I have used the type() function. The type() function returns the class type of the argument passed as a parameter.

Example:

a="hi"
print(type (a))
b=bytes(0)
print(type (b))
c=25
print(type (c))

To get the output, I have used print(type (a)) to get the class type of the first argument, and for the second argument. I have used print(type (b)), for the third argument I have used print(type(c)). You can refer to the below screenshot for the output.

Python string to byte type
Python string to byte type

You may like the following Python tutorials:

In this Python tutorial, we have learned about the Python string to a byte array. Also, We covered these below topics:

  • How to convert string to byte array encoding in Python
  • Python string to byte array UTF-8
  • Python string to byte array UTF-16
  • Python string to byte array hex
  • Python base64 string to byte array
  • Python binary string to byte array
  • Python JSON string to byte array
  • Python string to a byte array without b
  • Python hex string to byte array
  • Python string to byte array ASCII
  • Python 2.7 string to byte type