Signup/Sign In
PUBLISHED ON: MARCH 5, 2021

How to convert strings to bytes in python

In this tutorial, we will learn how to convert strings to bytes. We will convert the string to a bytes object using the byte() built-in function and encode() method.

In Python, bytes are just like an array. When we want to represent a group of byte values then we can consider bytes data types. The bytes data types allow values only from 0 to 255. The bytes data types are immutable. Even when no parameters passed to bytes, it returns an array of size zero. The encode() method encodes the specified string in the given encoding form.

Example 1: Converting a String to Byte

The below example shows how to convert the string to a bytes object using the byte() built-in function.

string="Hello Good Morning"
print("The given string is: ",string)
print("The type of given string is: ",type(string))

byte_object_1=bytes(string,"utf-8")
byte_object_2=bytes(string,"utf-16")

print("String converted to byte object with encoding utf-8: ",byte_object_1)
print("String converted to byte object with encoding utf-16: ",byte_object_2)

print("The converted string type with encoding utf-8 is: ",type(byte_object_1))
print("The converted string type with encoding utf-16 is: ",type(byte_object_1))

In the above code,

we consider string="Hello Good Morning", which is the source to be converted to bytes.

In the next step, using the built-in function bytes() and using the two encoding utf-8 and utf-16, we converted the string to bytes.Here, source= string , encoding= utf-8 and utf-16.

The type() function is used to check the type of object before and after the conversion of the string.


The given string is: Hello Good Morning
The type of given string is: <class 'str'>
String converted to byte object with encoding utf-8: b'Hello Good Morning'
String converted to byte object with encoding utf-16: b'\xff\xfeH\x00e\x00l\x00l\x00o\x00 \x00G\x00o\x00o\x00d\x00 \x00M\x00o\x00r\x00n\x00i\x00n\x00g\x00'
The converted string type with encoding utf-8 is: <class 'bytes'>
The converted string type with encoding utf-16 is: <class 'bytes'>

Example 2: Converting a String to Byte

The below example shows how to convert string to bytes by using encode(enc) function.

string="Learn coding in 2021"
print("The given string is: ",string)
print("The type of string is: ",type(string))
byte_object=string.encode("utf-8")
print("String converted to byte object using 'encode(enc)': ",byte_object)
print("The converted string type is: ",type(byte_object))

In the above code,

we consider string="Learn coding in 2021", which is the source to be converted to bytes.

In the next step, using the encode(enc) method, we converted the string to bytes. The type() function is used to check the type of object before and after the conversion of the string. Here, enc=utf-8.


The given string is: Learn coding in 2021
The type of string is: <class 'str'>
String converted to byte object using 'encode(enc)': b'Learn coding in 2021'
The converted string type is: <class 'bytes'>

Conclusion

In this tutorial. we learned how to convert the string object to bytes object using the in-built function bytes() and the encode() method.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.