In this Python string tutorial, you will learn how to convert string to bytes in Python.
Method 1
# Create a string S = "AiHints" # Convert string into bytes B = S.encode() # Print bytes print(B) # Display the type of B print(type(B))
Output:
b'AiHints' <class 'bytes'>
Method 2
# Create a string S = "AiHints" # string to bytes using bytes() B = bytes(S, encoding='utf-8') # Print bytes print(B) # Display the type of B print(type(B))
Output:
b'AiHints' <class 'bytes'>