This topic created in 3235 days ago, the information mentioned may be changed or developed.
python 中 unicode 和 str 到底有什么关系?为什么 Python3 中找不到 unicode 这个类,却可以用 u''来初始化变量?还是说一个 str 有多种编码方式,u'abc'就相当于'abc'.encode(),但是在 python3 中'abc'.encode('unicode')这种参数是不存在的,这是怎么回事?
从 len('汉')=3,len(u'汉')=1 的结果看出,str 是默认 utf-8 编码的?
 |
|
1
zhihhh Aug 5, 2017
在 python3 里面所有的 str 都默认是 unicode 了。不存在有'abc'.endcode('unicode')这种说法了吧。
|
 |
|
2
gdsing Aug 5, 2017
不清楚楼主到底是想问 py3,还是 py2。 1、如楼上说的,py3 默认都是 unicode 2、len('汉')=3 可能会出现 len('汉')=2 的情况,py2 str 编码是跟系统的。
|
 |
|
3
pia Aug 5, 2017
还有'unicode'参数?是 'abc'.encode('utf-8') 吧
|