官术网_书友最值得收藏!

Strings with Unicode

Strings are fully Unicode capable, so you can use them with international characters easily, even in literals, because the default source code encoding for Python 3 is UTF-8. For example, if you have access to Norwegian characters, you can simply enter this:

>>> "Vi er s? glad for ? h?re og l?re om Python!"
'Vi er s? glad for ? h?re og l?re om Python!'

Alternatively, you can use the hexadecimal representations of Unicode code points as an escape sequence prefixed by \u:

>>> "Vi er s\u00e5 glad for \u00e5 h\xf8re og l\u00e6re om Python!"
'Vi er s? glad for ? h?re og l?re om Python!'

We're sure you'll agree, though, that this is somewhat more unwieldy.

Similarly, you can use the \x escape sequence followed by a 2-character hexadecimal string to include one-byte Unicode code points in a string literal:

>>> '\xe5'
'?'

You can even an use an escaped octal string using a single backlash followed by three digits in the range zero to seven, although we confess we've never seen this used in practice, except inadvertently as a bug:

>>> '\345'
'?'

There are no such Unicode capabilities in the otherwise similar bytes type, which we'll look at next.

主站蜘蛛池模板: 化州市| 鄂伦春自治旗| 宝山区| 东乌| 芮城县| 垦利县| 富顺县| 台江县| 南华县| 兴文县| 安宁市| 玉溪市| 湘潭市| 宣城市| 宜兴市| 竹北市| 汉源县| 天全县| 乌拉特前旗| 兴山县| 塔河县| 昂仁县| 长子县| 汉寿县| 泽州县| 常宁市| 兴仁县| 开原市| 旺苍县| 昌吉市| 青浦区| 株洲县| 嘉禾县| 右玉县| 怀远县| 永宁县| 大兴区| 鄱阳县| 宁都县| 十堰市| 德钦县|