在 Python 原始字符串的上下文中 [英] In context of Python Raw string
问题描述
我的 Python 版本是:
~$ python --version蟒蛇 2.6.6
我尝试在 Python 中进行以下操作(我想展示所有内容):
1: \
用作转义序列
2: \
用作转义序列
3: str()
并打印为值而不是类型
4:它的 Python 是一个原始字符串
<预><代码>>>>repr('让我们用 Python')'让我们用 Python"'[问题]
5:Python 原始字符串
<预><代码>>>>打印 r'Let\'s Python'让我们的 Python6:这个,我不明白以下内容:
<预><代码>>>>r'Let's Python'让我们的 Python">>>r'\\''\\\\'为什么是 \\
?为什么5
和6
的输出不同?r
和 repr()
一样不一样?
还请说明string
和raw strings
的内部表示相同或不同.
您将原始字符串文字 r''
与字符串表示混淆了.有一个很大的不同.repr()
和 r''
不是一回事.
r''
原始字符串文字产生一个字符串,就像普通字符串文字一样,除了它如何处理转义码.产生的结果仍然是一个python字符串.您可以使用原始字符串文字或普通字符串文字生成相同的字符串:
当不使用 r''
原始字符串文字时,我必须将斜杠加倍才能转义它,否则它会被解释为换行符.我在使用 r''
语法时不必转义它,因为它根本不解释诸如 \n
之类的转义码.输出,结果python字符串value,完全一样:
解释器使用 repr()
将这些值回传给我们;生成python值的表示:
注意 repr()
结果如何包含引号.当我们只回显字符串的repr()
时,结果是本身 一个字符串,因此它有两组 引号.另一个"
引号标记了repr()
结果的开始和结束,并包含在内,即python字符串的字符串表示字符串
.
所以r''
是一种生成python字符串的语法,repr()
是一种生成表示python值的字符串的方法.repr()
也适用于其他 python 值:
1
整数表示为字符串 '1'
(字符串中的字符 1
).
My Python version is:
~$ python --version
Python 2.6.6
I tried following in Python (I wants to show all):
1: \
use as escape sequence
>>> str('Let\'s Python')
"Let's Python"
2: \
use as escape sequence
>>> 'Let\'s Python'
"Let's Python"
3: str()
and print as value not type
>>> print 'Let\'s Python'
Let's Python
4: its Python a raw string
>>> repr('Let\'s Python')
'"Let\'s Python"'
[QUESTION]
5: Python raw string
>>> print r'Let\'s Python'
Let\'s Python
6: This, I do not understand followings:
>>> r'Let\'s Python'
"Let\\'s Python"
>>> r'\\'
'\\\\'
Why \\
? Why output in 5
and 6
are different?
r
and repr()
are same not same?
Also please explain about internal representation of string
and raw strings
are same or different.
You are confusing raw string literals r''
with string representations. There is a big difference. repr()
and r''
are not the same thing.
r''
raw string literals produce a string just like a normal string literal does, with the exception to how it handles escape codes. The produced result is still a python string. You can produce the same strings using either a raw string literal or a normal string literal:
>>> r'String with \n escape ignored'
'String with \\n escape ignored'
>>> 'String with \\n escape ignored'
'String with \\n escape ignored'
When not using a r''
raw string literal I had to double the slash to escape it, otherwise it would be interpreted as the newline character. I didn't have to escape it when using the r''
syntax because it does not interpret escape codes such as \n
at all. The output, the resulting python string value, is exactly the same:
>>> r'String with \n escape ignored' == 'String with \\n escape ignored'
True
The interpreter is using repr()
to echo those values back to us; the representation of the python value is produced:
>>> print 'String'
String
>>> print repr('String')
'String'
>>> 'String'
'String'
>>> repr('String')
"'String'"
Notice how the repr()
result includes the quotes. When we echo just the repr()
of a string, the result is itself a string, so it has two sets of quotes. The other "
quotes mark the start and end of the result of repr()
, and contained within that is the string representation of the python string String
.
So r''
is a syntax to produce python strings, repr()
is a method to produce strings that represent a python value. repr()
also works on other python values:
>>> print repr(1)
1
>>> repr(1)
'1'
The 1
integer is represented as a string '1'
(the character 1
in a string).
这篇关于在 Python 原始字符串的上下文中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!