【发布时间】:2012-08-27 21:02:31
【问题描述】:
作为标题,是否有理由不使用 str() 将 unicode 字符串转换为 str??
>>> str(u'a')
'a'
>>> str(u'a').__class__
<type 'str'>
>>> u'a'.encode('utf-8')
'a'
>>> u'a'.encode('utf-8').__class__
<type 'str'>
>>> u'a'.encode().__class__
<type 'str'>
更新:感谢您的回答,也不知道我是否使用特殊字符创建了一个字符串,它会自动转换为 utf-8
>>> a = '€'
>>> a.__class__
<type 'str'>
>>> a
'\xe2\x82\xac'
也是python 3中的Unicode对象
【问题讨论】: