python2和python3中str,bytes区别

时间:2021-05-12 20:14:41

python2中,有basestring、str、bytes、unicode四种类型

其中str == bytes ,basestring = (str,unicode)

>>> isinstance('s',str)
True
>>> isinstance('s',bytes)
True
>>> isinstance('s',unicode)
False
>>> isinstance('s'.decode(),unicode)
True
>>> isinstance('s'.decode(),basestring)
True
>>> isinstance('s',basestring)
True
 

 

python3中,有str和bytes类型

>>> isinstance('s',str)
True
>>> isinstance('s',bytes)
False
>>> isinstance('s',unicode)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'unicode' is not defined
>>> isinstance(b's',bytes)
True
>>> isinstance('s',basestring)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'basestring' is not defined