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:

πŸ’» Awesome Tech Resources:
  • Looking for βš’οΈ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events πŸ—“οΈ Calendar.️

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.

Related Posts

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.