Python String to bytes

In this post, we will see how to convert String to bytes in Python.

There are two ways to convert String to bytes in Python.

Using bytes() constructor

You can use bytes() constructor to convert String to bytes in Python. You need to pass string and encoding as argument.
Here is an example:

Output:

b’java2blog’

Using encode() method

You can also you string’s encode() method to convert String to byte.

Output:

b’python’

That’s all about Python String to bytes.

Was this post helpful?

Leave a Reply

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