简体   繁体   English

如果默认编码为ascii,为什么在utf-8中编码字符串? -蟒蛇

[英]why are strings encoded in utf-8 if the default encoding is ascii? - python

In python, when I import sys and type: 在python中,当我import sys并键入:

>>> sys.getdefaultencoding()
>>> 'ascii'

why is this string automatically encoded as UTF-8? 为什么此字符串会自动编码为UTF-8?

>>> a = 'ö'
>>> a
>>> '\xc3\xb6'

Because the input you provided to python was 因为您提供给python的输入是

 a        =       '   ö       '
\x61\x20\x3d\x20\x27\xc3\xb6\x27

You told a to contain the byte sequence "\\xc3\\xb6" by putting those two bytes between the quotes in your console input, and so it does. 您通过在控制台输入中的引号之间将这两个字节放在引号之间,让a包含字节序列"\\xc3\\xb6" ,所以它确实如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM