使用Python 2.7,我想知道使用类型unicode而不是str因为它们两者似乎能够保存Unicode字符串的真正优势.除了能够unicode使用转义字符串在字符串中设置Unicode代码之外,还有什么特殊原因\吗?:
执行模块:
# -*- coding: utf-8 -*-
a = 'á'
ua = u'á'
print a, ua
Run Code Online (Sandbox Code Playgroud)
结果:á,á
编辑:
使用Python shell进行更多测试:
>>> a = 'á'
>>> a
'\xc3\xa1'
>>> ua = u'á'
>>> ua
u'\xe1'
>>> ua.encode('utf8')
'\xc3\xa1'
>>> ua.encode('latin1')
'\xe1'
>>> ua
u'\xe1'
Run Code Online (Sandbox Code Playgroud)
因此,unicode字符串似乎使用latin1而不是编码utf-8,原始字符串使用utf-8?我现在更加困惑了!:S