I want to convert a string
'hello world'
to
b'hello world'
in python, how can I do that?
I want to convert a string
'hello world'
to
b'hello world'
in python, how can I do that?
You can use the str.encode()
method to convert a string to bytes.
>>> 'hello world'.encode()
b'hello world'
>>>
Please refer to: https://docs.python.org/3/library/stdtypes.html#str.encode
You can use b = bytes(mystring, 'utf-8')
notation to turn your string into bytes object. But there are many similar questions and related answers in stackoverflow to your question. You should have searched for your question first.