Menu Close

How to convert string to binary with Python?

Sometimes, we want to convert string to binary with Python.

In this article, we’ll look at how to convert string to binary with Python.

How to convert string to binary with Python?

To convert string to binary with Python, we call the encode method.

For instance, we write

b = "hello world".encode('ascii')                           

to call 'hello world'.encode with 'ascii' to get an ASCII binary string with the same content.

Conclusion

To convert string to binary with Python, we call the encode method.

Posted in Python, Python Answers