2

这个有效(仅一行):

   c.execute(f"SELECT Firm, Platform, `Sale in million` FROM database WHERE Platform IN  ({','.join('?' for _ in list_table)}) ORDER BY Firm", (list_table))

但这些没有(2行):

c.execute(f"SELECT Firm, Platform, `Sale in million` FROM database 
          WHERE Platform IN  ({','.join('?' for _ in list_table)}) ORDER BY Firm", (list_table))

c.execute(f"SELECT Firm, Platform, `Sale in million` FROM database" 
          f"WHERE Platform IN  ({','.join('?' for _ in list_table)}) ORDER BY Firm", (list_table))"

c.execute("SELECT Firm, Platform, `Sale in million` FROM database" 
          f"WHERE Platform IN  ({','.join('?' for _ in list_table)}) ORDER BY Firm", (list_table))"

如何在多行中使用 f-string?

4

1 回答 1

1

问题源于本质上无效的多行,而不是f-string 的任何错误。. 请注意,您只需要在括号内使用您的变量,而不是使用语法{}单独传递它们。f-string在字符串末尾使用三引号或反斜杠以使用多行字符串。链接到文档

test = "this is a 
       bad multiline " #Raises SyntaxError: EOL while scanning string literal

test = ''' this is a 
         valid multiline'''

a = "answer"

print(f'''test
        is complete. check {a} ''')
于 2019-01-09T19:08:54.810 回答