为什么我的python怎么都定义不了byte类型的变量
看到网上定义byte都是输出b''这种格式
a = '123'
type(a)
Python没有这个类型吧
st = "good"
st
'good'data = bytes(st,"ascii")
data
b'good'
应该是版本的问题,我的2.7的也这样,3.0以上不会
引用网上的:Python 2有基于ASCII的str()类型,其可通过单独的unicode()函数转成unicode类型,但没有byte类型。
而在Python 3中,终于有了Unicode(utf-8)字符串,以及两个字节类:bytes和bytearrays。
地址:https://www.cnblogs.com/chownjy/p/6625299.html
以及http://www.jb51.net/article/57956.htm#unicode
a1= '123'
type(a1)
在 python2 中 bytes 和 str 是同一种类型,str 又是 python2 的原生字符串类型,所以输出前面没有前缀“b”。
Python 2.7.14+ (default, Feb 6 2018, 19:12:18)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> bytes == str
True
>>> type(bytes('abc'))
<type 'str'>
python3中,bytes 和 str 不再是同一种类型。bytes相当于 python2 中的 bytes 和 str,
str 相当于 python2 中 的unicode。str(相当于python2 中的uncoded)是python3中的原生
字符串类型,输出没有前缀,bytes类型的数据输出会加前缀 b。
Python 3.6.4+ (default, Feb 12 2018, 08:25:03)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> str
<class 'str'>
>>> bytes
<class 'bytes'>
>>> type('str')
<class 'str'>
>>> type(b'bytes')
<class 'bytes'>
a1=b'\x111'
type(a1)
python3没有byte类型,只有bytes类型