How to convert a string to bytes in Python?

In this article, we will discuss what are Bytes and String, and also we will learn how to convert string to bytes using python.

Table Of Contents

Before Python3, the strings and bytes were of same object type, which is type Byte. But now in Python 3, we have Bytes which are sequence of Bytes and strings are sequence of characters. Strings are not machine readable. In order to store them on disk we need to convert them to bytes.

What are Strings an Bytes ?

Strings

A String is an array of bytes representing Unicode characters enclosed in single, double or triple quotes. The Enclosed characters can be any digit, alphabets or special symbols. A String is just normal text in human readable format. Also, strings are immutable, it means once defined then they can not be changed.

Example :

strValue = 'String Example'
print(strValue)

# type() will print the data type
print(type(strValue)) 

Output :

String Example
<class 'str'>

Bytes

Whenever we find a prefix ‘b’ in front of any string, it is reffered as byte string in python. Bytes are not human readable, machines like our computers can understand them easily and interprets them as human readable.

Example

byteValues = b'Bytes example'
print(byteValues)

# type() will print the data type
print( type(byteValues) ) 

Output :

b'Bytes example'
<class 'bytes'>

So, we know about strings and bytes data types. Now we will look into the methods through which we can covert strings to bytes. We have different methods for this conversion in python, we will look them one by one.

Always try examples in your machine. Just copy and paste code and play around with it. We have used Python 3.10.1 for writing example codes. To check your version write python –version in your terminal.

Convert String to Bytes using bytes() method

The bytes() method is a built-in method in Python, and it recieves three parameters :

  • First is string which needs to converted to bytes.
  • Second is method of encoding. Here we will be using utf-8. You need to provide a encoding method otherwise it will throw TypeError .
    • There are other methods of encoding like UTF-16,Latin-1.Feel free to use other encoding methods depending on your use.
  • Third is error handling , default is ‘strict’.Other methods of handling are ‘ignore’ , ‘replace’.

SYNTAX:

bytes(str, encoding,error)

EXAMPLE :

strValue = 'I am Happy ?'
print(strValue)

# type() will print data type of strValue
print(type(strValue))

# Convert string to bytes
bytesValue = bytes(strValue,'UTF-8')

print(bytesValue)

# type() will print data type of bytesValue
print(type(bytesValue))

OUTPUT :

I am Happy ?
<class 'str'>

b'I am Happy \xf0\x9f\x98\x8a'
<class 'bytes'>

You can see we have used byte() method to convert string to bytes.

Convert String to Bytes using encode() method

The encode() is a built-in method of Python, and it is most commonly used to convert bytes to string. As we know that the word encode means encrypting, which means to encrypt a data to machine readable format, that cannot easily be understood by humans.

It recieves two parameters :
– First is the encoding method which is optional in encode() method and in python 3 default method of encoding is ‘UTF-8’.
– Second is error handling or a error message in form of string which is also an optional.

SYNTAX :

str.encode(encoding='UTF-8', error)

str here is string variable which needs to be converted to bytes.

EXAMPLE :

strValue = 'I am using encode method ??'
print(strValue)

#type() will output the data type of strValue 
print(type(strValue))

# Convert string into bytes using encode() method
bytesValue = strValue.encode()

# type() will output the data type of bytesValue
print(type(bytesValue))

print(bytesValue)

OUTPUT :

I am using encode method ??
<class 'str'>

<class 'bytes'>
b'I am using encode method \xf0\x9f\x91\x87\xf0\x9f\x91\x87'

So here we used encode() method to convert strings to bytes.

Summary

In this article we used two different methods to convert a given string into bytes data type. You can always use both but the easiest and most common used method is encode() method, because you do not need to provide any error handling or encoding method in it. But if you don’t provide any of these in bytes() method, you will face TypeError .

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top