Python中unicode字符串的问题

时间:2022-03-08 22:34:04

when learning the unicode from python document, I am trying to reproduce the following example

当从python文档学习unicode时,我试图重现以下示例

>>> ur'Hello\u0020World !'
  u'Hello World !'
>>> ur'Hello\\u0020World !'
u'Hello\\\\u0020World !'

I use PyCharm as editor, the Python version is 2.7, However, what I get is

我使用PyCharm作为编辑器,Python版本是2.7,但是,我得到的是

>>>ur'Hello\u0020World !'
Hello World !
>>>ur'Hello\\u0020World !'
Hello\\u0020World !

I don't know why the second one is different with what Python document said. What caused the difference?

我不知道为什么第二个与Python文档所说的有所不同。是什么造成了差异?

1 个解决方案

#1


Notice in the first case the printed result is quoted like "u'hello....'" This indicates it is printing a Python string literal. The "r" prefix allows you to put backslash in a string without escaping it. Since the result is "u" alone, each backslash must be escaped.

请注意,在第一种情况下,打印结果的引用类似于“u'hello ....”这表示它正在打印Python字符串文字。 “r”前缀允许您在字符串中放入反斜杠而不转义它。由于结果仅为“u”,因此必须转义每个反斜杠。

In the second case, the output isn't quoted - so it is the string itself, and not a string literal as it would appear in Python source code. This shows exactly what the string is, and not the string as it must be encoded to be in Python source code.

在第二种情况下,输出没有引用 - 所以它是字符串本身,而不是像Python源代码中出现的字符串文字。这显示了字符串的确切内容,而不是字符串,因为它必须编码为Python源代码。

#1


Notice in the first case the printed result is quoted like "u'hello....'" This indicates it is printing a Python string literal. The "r" prefix allows you to put backslash in a string without escaping it. Since the result is "u" alone, each backslash must be escaped.

请注意,在第一种情况下,打印结果的引用类似于“u'hello ....”这表示它正在打印Python字符串文字。 “r”前缀允许您在字符串中放入反斜杠而不转义它。由于结果仅为“u”,因此必须转义每个反斜杠。

In the second case, the output isn't quoted - so it is the string itself, and not a string literal as it would appear in Python source code. This shows exactly what the string is, and not the string as it must be encoded to be in Python source code.

在第二种情况下,输出没有引用 - 所以它是字符串本身,而不是像Python源代码中出现的字符串文字。这显示了字符串的确切内容,而不是字符串,因为它必须编码为Python源代码。