作为标题,是否有理由不使用 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 对象