# old-style string interpolation
open((r'E:\Dp\Python\Yt\%s.txt' % title), 'w')
# or str.format, which is VASTLY preferred in any modern Python
open(r'E:\Dp\Python\Yt\{}.txt'.format(title), 'w')
# or, in Python 3.6+, the new-hotness of f-strings
open(rf'E:\Dp\Python\Yt\{title}.txt', 'w')
需要转义他们所有的斜杠博约,否则他们将转义后面的字符。在
问题中突出显示的语法应该能帮助你。
\'
不终止字符串它是一个转义的单勾号'
。在正如this old answer所示,实际上不可能用反斜杠结束原始字符串。最好的选择是使用字符串格式
或者添加更多的字符串连接,这看起来更糟。在
^{pr2}$相关问题 更多 >
编程相关推荐