Python string to bytes code example
Example 1: bytes to string python
bytes = b'abcde'
bytes.decode("utf-8")
'abcde'
Example 2: python string to bytes
s = 'abc'
b = bytes(s, encoding='utf-8')
print(type(b))
print(b)
s = b.decode()
print('Original String =', s)
s = 'xyz'
b = s.encode(encoding='utf-8')
print(b)
s = b.decode()
print('Original String =', s)
Example 3: python convert string to bytes
data = ""
data = "".encode()
data = b""
data = b"".decode()
data = str(b"")
Example 4: python convert string to byte array
a_string = "abc"
encoded_string = a_string. encode()
byte_array = bytearray(encoded_string)
print(byte_array)