How to Convert String to Bytes in Python

The process of converting Python string to bytes is called encoding, and converting bytes to string is called decoding.

A byte object in Python is a sequence of bytes, and these byte objects are machine-readable and can be instantly stored on the disk. Strings are human-readable forms that need to be encoded and stored on a disk.

Python String to Bytes

To convert string to bytes in Python, use the encode() method. The string.encode(encoding) method is a more Pythonic way than using the bytes() constructor because it is the most self-documenting and suggesting way of saying that “take this string and encode it with this encoding standard,” which is clearer than bytes(string, encoding).

But still, If you want to use the bytes constructor, then pass the string as the first input of the constructor of the Bytes and then pass the encoding standard as the second argument.

Code snippet

string.encode("UTF-8")

# OR

bytes(string, "UTF-8")

The argument of string.encode() method is encoding standard.

The bytes() function takes two arguments; the first is a string, which we will pass to convert into bytes.

The second argument is an encoding standard, UTF-8 or ASCII.

Convert string to bytes using Python string encode()

See the following code.

string = "Cyberpunk 2077"

# String with encoding "UTF-8"
p_bytes = string.encode("UTF-8")

# String with encoding "ASCII"
p_bytes2 = string.encode("ASCII")

print(p_bytes, '\n')
print(p_bytes2, "\n")

# Actual bytes in the the string
for byte in p_bytes:
    print(byte, end=' ')
print("\n")
for byte in p_bytes2:
    print(byte, end=' ')

Output

b'Cyberpunk 2077'

b'Cyberpunk 2077'

67 121 98 101 114 112 117 110 107 32 50 48 55 55

67 121 98 101 114 112 117 110 107 32 50 48 55 55

In the first step, we defined a string and then used the string encode() function to encode the string into bytes using two standards: 1) UTF-8 and 2) ASCII. Then we printed both bytes and then used the for-in loop to print the byte one by one.

In Python3, the best way to encode a string is by using the encode() function with the default argument, “UTF-8”.

You can see from the output that the first two outputs are b-strings, meaning they are now bytes. Python b string consists of bytes data, meaning the literal representing integers are between and 255.

Convert string to bytes using Python bytes constructor

See the following code.

string = "Cyberpunk 2077"

# String with encoding "UTF-8"
p_bytes = bytes(string, "UTF-8")

# String with encoding "ASCII"
p_bytes2 = bytes(string, "ASCII")

print(p_bytes, '\n')
print(p_bytes2, "\n")

# Actual bytes in the the string
for byte in p_bytes:
    print(byte, end=' ')
print("\n")
for byte in p_bytes2:
    print(byte, end=' ')

Output

b'Cyberpunk 2077'

b'Cyberpunk 2077'

67 121 98 101 114 112 117 110 107 32 50 48 55 55

67 121 98 101 114 112 117 110 107 32 50 48 55 55

In the first step, we defined a string and then used the bytes constructor to encode the string into bytes using two standards: 1) UTF-8 and 2) ASCII. Then we printed both bytes and then used the for-in loop to print the byte one by one.

That is it for this tutorial. Thanks for taking it.

See also

Python raw string

Python multiline string

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.