Python 3.x 如何在python中连接字符串

Python 3.x 如何在python中连接字符串,python-3.x,string,Python 3.x,String,new=“file.txt” source\u path=r“C:\Users\Shri Krishan\Documents”+“\”+新建 打印(源路径) 错误:语法错误:扫描字符串文字时下线代码: new = "file.txt" source_path = r"C:\Users\Shri Krishan\Documents" + "\\" + new print(source_path) 结果: C:\Users\Shri

new=“file.txt”

source\u path=r“C:\Users\Shri Krishan\Documents”+“\”+新建

打印(源路径)

错误:语法错误:扫描字符串文字时下线代码:

new = "file.txt"

source_path = r"C:\Users\Shri Krishan\Documents" + "\\" + new

print(source_path)
结果:

C:\Users\Shri Krishan\Documents\file.txt

要处理文件和目录路径,我建议使用或更近的,而不是使用字符串连接。稍微解释一下就好了。还有,为什么要连接两个字符串文字?您可以简单地编写
r“C:\Users\Shri Krishan\Documents\”+new