[英]why does python add another backslash to \i
I thought I understood backslash escaping, but I don't get this behaviour: 我以为我知道反斜杠转义,但是我没有这种行为:
foo = '\t'
gives the same string (ie \\t
) when I type foo
in the Python interpreter, whereas 当我在Python解释器中输入
foo
时,给出相同的字符串(即\\t
),而
bar = '\i'
gives \\\\i
. 给出
\\\\i
。 What's happening? 发生了什么? I want to have just
\\i
, because I'm writing it to a .tex
file that I'm then compiling, and this seems to mess up the LaTeX commands. 我只想拥有
\\i
,因为我正在将其写入要编译的.tex
文件中,这似乎弄乱了LaTeX命令。
Edit: it was in fact not this that was messing up my latex, as the answers below show 编辑:实际上不是这弄乱了我的乳胶,如下面的答案所示
"\\t"
is a tab (one character), "\\i"
contains two characters, the first of which is escaped by __repr__
. "\\t"
是一个制表符(一个字符), "\\i"
包含两个字符,第一个由__repr__
转义。
In [51]: len("\t")
Out[51]: 1
In [52]: len("\i")
Out[52]: 2
Edit: If you write to a file, your output will be fine. 编辑:如果您写入文件,您的输出将很好。
with open("o.tex", "w") as o:
o.write(">>>\t|\i|\t<<<\n")
The content of o.tex
will then be 然后
o.tex
的内容将是
>>> |\i| <<<
You can see the two tabs \\t
as whitespace while all other characters are as-is. 您可以看到两个制表符
\\t
为空格,而所有其他字符均保持原样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.