How to convert string to bytes in Python 3

TypeError: 'str' does not support the buffer interface suggests two possible methods to convert a string to bytes:

b = bytes(mystring, 'utf-8')

b = mystring.encode('utf-8')

What are the differences between them? Which one should I opt for and why?


See Convert bytes to a string in Python 3 for the other way around.

← Назад к списку