python3 中如何进bytes串转换成str?

比如:a = b✀123✀想变成:a = ✀123✀
2025-04-15 09:25:04
推荐回答(3个)
回答1:

bytes解码会得到str

str编码会变成bytes

>>> b'123'.decode('ascii')
'123'
>>> '123'.encode('ascii')
b'123'

回答2:

Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
[GCC 6.3.0 20170118] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = b'123'
>>> a.decode()
'123'

回答3:

b'123'.decode()
结果是'123'