We can use the built-in Bytes class in Python to convert a string to bytes: simply pass the string as the first input of the constructor of the Bytes class and then pass the encoding as the second argument.
Printing the object shows a user-friendly textual representation, but the data contained in it is in bytes.
string = "Hello World"
# string with encoding 'utf-8'
arr = bytes(string, 'utf-8')
arr2 = bytes(string, 'ascii')
print(arr,'\n')
# actual bytes in the the string
for byte in arr:
print(byte, end=' ')
print("\n")
for byte in arr2:
print(byte, end=' ')