我想指定一个包含行继续符和连接符的字符串。如果我要回应一堆相关值,这真的很有用。这是一个只有两个参数的简单示例:
temp = "here is\n"\
+"\t{}\n"\
+"\t{}".format("foo","bar")
print(temp)
这是我得到的:
here is
{}
foo
这是我所期望的:
here is
foo
bar
什么给了?
最佳答案
你可以尝试这样的事情:
temp = ("here is\n"
"\t{}\n"
"\t{}".format("foo","bar"))
print(temp)
或者喜欢:
# the \t have been replaced with
# 4 spaces just as an example
temp = '''here is
{}
{}'''.format
print(temp('foo', 'bar'))
对比你有什么:
a = "here is\n"
b = "\t{}\n"
c = "\t{}".format("foo","bar")
print( a + b + c)
关于具有字符串连接和延续的 Python str.format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48508171/