Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python str.format,带字符串连接和延续_Python_String_Format_Concatenation_Continuation - Fatal编程技术网

Python str.format,带字符串连接和延续

Python str.format,带字符串连接和延续,python,string,format,concatenation,continuation,Python,String,Format,Concatenation,Continuation,我想指定一个同时包含行连续字符和连接字符的字符串。如果我回应一堆相关的值,这真的很有用。下面是一个简单的示例,只有两个参数: temp = "here is\n"\ +"\t{}\n"\ +"\t{}".format("foo","bar") print(temp) 以下是我得到的: here is {} foo 以下是我的期望: here is foo bar 有什么好处?您可以尝试以下方法: temp = ("here is\n"

我想指定一个同时包含行连续字符和连接字符的字符串。如果我回应一堆相关的值,这真的很有用。下面是一个简单的示例,只有两个参数:

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)

format函数仅应用于最后一个字符串

temp = "here is\n"\
    +"\t{}\n"\
    +"\t{}".format("foo","bar")
正在这样做:

temp = "here is\n" + "\t{}\n"\ + "\t{}".format("foo","bar")
关键是
.format()
函数只发生在最后一个字符串上:

"\t{}".format("foo","bar")
您可以使用括号获得所需的结果:

temp = ("here is\n"\
    +"\t{}\n"\
    +"\t{}").format("foo","bar")
print(temp)

#here is
#   foo
#   bar

str.format
在串接字符串之前被调用。把它想象成
1+2*3
,乘法在加法之前求值

在调用
str.format
之前,只需将整个字符串用括号括起来,表示您希望字符串连接起来:

temp = ("here is\n"
      + "\t{}\n"
      + "\t{}").format("foo","bar")

Python实际上看到了这一点:

Concatenate the result of
    "here is\n"
with the resuslt of
    "\t{}\n"
with the result of
    "\t{}".format("foo","bar")
您有3个单独的字符串文本,只有最后一个应用了
str.format()
方法

请注意,Python解释器在运行时连接字符串

您应该改为使用隐式字符串文字连接。每当您在表达式中并排放置两个字符串文字,且其间没有其他运算符时,就会得到一个字符串:

"This is a single" " long string, even though there are separate literals"
这与字节码一起存储为单个常量:

>>> compile('"This is a single" " long string, even though there are separate literals"', '', 'single').co_consts
('This is a single long string, even though there are separate literals', None)
>>> compile('"This is two separate" + " strings added together later"', '', 'single').co_consts
('This is two separate', ' strings added together later', None)
从:

允许多个相邻的字符串或字节文字(由空格分隔),可能使用不同的引用约定,其含义与它们的串联相同。因此,
“hello”‘world’
相当于
“helloworld”

使用隐式字符串文字连接时,末尾的任何
.format()
调用都将应用于整个单个字符串

接下来,您不想使用
\
反斜杠行继续。使用括号代替,它更干净:

temp = (
    "here is\n"
    "\t{}\n"
    "\t{}".format("foo","bar"))
这就是所谓的

您可能还想了解多行字符串文字,其中在开始和结束处使用三个引号。这些字符串中允许使用换行符,并且换行符仍然是值的一部分:

temp = """\
here is
\t{}
\t{}""".format("foo","bar")

我在开头的
后使用了一个
\
反斜杠来转义第一个换行。

您的第二个解决方案将有2个额外的“\n”,您需要删除2个键入的“\n”“。对于第二个代码块,您必须使用
temp='\
使空白保持一致。只需删除
+
串联,并将其留给编译器来串联文本。@MartijnPieters:如果这意味着代码更清晰,我将承担性能损失。”。隐式字符串连接给我带来了太多的问题。