hanfeisun hanfeisun - 2 years ago 122
Python Question

How to convert 'binary string' to normal string in Python3?

For example, I have a string like this(return value of


>>> b'a string'
b'a string'

Whatever I did to it, it is always printed with the annoying
before the string:

>>> print(b'a string')
b'a string'
>>> print(str(b'a string'))
b'a string'

Does anyone have any ideas about how to use it as a normal string or convert it into a normal string?

Answer Source

Decode it.

>>> b'a string'.decode('ascii')
'a string'

To get bytes from string, encode it.

>>> 'a string'.encode('ascii')
b'a string'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download