简体   繁体   English

字符串编码中的Python-3和\\ x Vs \\ u Vs \\ U及其原因

[英]Python-3 and \x Vs \u Vs \U in string encoding and why

Why do we have different byte oriented string representations in Python 3? 为什么我们在Python 3中有不同的面向字节的字符串表示? Won't it be enough to have single representation instead of multiple? 单个表示而不是多个表示是否足够?

For ASCII range number printing a string shows a sequence starting with \\x : 对于ASCII范围编号打印,字符串显示以\\x开头的序列:

 In [56]: chr(128)
 Out[56]: '\x80'

In a different range of numbers it Python uses a sequence starting with \\u\u003c/code> 在不同的数字范围内,Python使用以\\u\u003c/code>开头的序列

In [57]: chr(57344)
Out[57]: '\ue000'

But numbers in the highest range, ie the maximum Unicode number as of now, it uses a leading \\U : 但是在最高范围内的数字,即截至目前的最大Unicode数,它使用前导\\U

In [58]: chr(1114111)
Out[58]: '\U0010ffff'

Python gives you a representation of the string, and for non-printable characters will use the shortest available escape sequence . Python为您提供字符串的表示,对于不可打印的字符,将使用最短的可用转义序列

\\x80 is the same character as or \\U00000080 , but \\x80 is just shorter. \\x80\\U00000080是相同的字符,但\\x80只是更短。 For chr(57344) the shortest notation is \ , you can't express the same character with \\xhh , that notation only can be used for characters up to \\0xFF . 对于chr(57344) ,最短的符号是\ ,你不能用\\xhh表示相同的字符,表示法只能用于最多为\\0xFF字符。

For some characters there are even single-letter escapes, like \\n for a newline, or \\t for a tab. 对于某些字符,甚至还有单字母转义符,例如\\n表示换行符,或\\t表示制表符。

Python has multiple notation options for historical and practical reasons. 出于历史和实际原因,Python有多种表示法选项。 In a byte string you can only create bytes in the range 0 - 255, so there \\xhh is helpful and more concise than having to use \\U000hhhhh everywhere when you can't even use the full range available to that notation, and \\xhh and \\n and related codes are familiar to programmers from other languages. 在一个字节串 ,你只能在0范围内创建字节- 255,所以\\xhh是有益的,不必使用更简洁\\U000hhhhh无处不在的时候,你甚至不能使用可用的全方位该符号,和\\xhh\\n及相关代码对其他语言的程序员来说很熟悉。

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

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