converting string to byte in python code example

Example 1: bytes to string python

# utf-8 is used here because it is a very common encoding, but you
# need to use the encoding your data is actually in.
bytes = b'abcde'
bytes.decode("utf-8") 
'abcde'

Example 2: python convert string to bytes

data = ""  			#string
data = "".encode()	#bytes
data = b"" 			#bytes
data = b"".decode() #string
data = str(b"")  	#string

Example 3: python convert string to byte array

a_string = "abc"
encoded_string = a_string. encode()
byte_array = bytearray(encoded_string)
print(byte_array)