相关疑难解决方法(0)

为什么在 Python 中打印元组(列表、字典等)时会加倍反斜杠?

在Python中,当我打印带有反斜杠的字符串时,它只打印一次反斜杠:

>>> print(r'C:\hi')
C:\hi
>>> print('C:\\hi')
C:\hi
Run Code Online (Sandbox Code Playgroud)

但我注意到,当您打印带有反斜杠的字符串元组时,它会打印双反斜杠:

>>> print((r'C:\hi', 'C:\\there'))
('C:\\hi', 'C:\\there')
Run Code Online (Sandbox Code Playgroud)

为什么打印元组时表现不同?

(请注意,这种情况在 Python 2 和 3 以及 Windows 和 Linux 中都会发生。)

python printing escaping backslash

5
推荐指数
1
解决办法
1701
查看次数

标签 统计

backslash ×1

escaping ×1

printing ×1

python ×1