- 我知道有几种方法可以创建多行字符串:
三重引号
'''
This is a
multi-line
string.
'''
连接字符串
('this is '
'a string')
转义字符
'This is'\
'a string'
I also know that prefixing the string with
r
will make it a raw string, useful for filepaths.r'C:\Path\To\File'
然而,我有一个很长的文件路径,跨越多行并且需要是原始字符串。我该怎么办?
以下代码可行:
In [1]: (r'a\b'
...: '\c\d')
Out[1]: 'a\\b\\c\\d'
但出于某些原因,这并不起作用:
In [4]: (r'on\e'
...: '\tw\o')
Out[4]: 'on\\e\tw\\o'
为什么
"t"
只有一个反斜杠?
r'''...'''
可以很好地生成一个多行的原始字符串。 - jasonharper\n
来表示新行:In [7]: r'''path\to ...: \file''' Out[7]: 'path\\to\n\\file'
- Josh D