python 3 中带有变量组的多行 .format() 方法

Multiline .format() method with group of variables in python 3

这按原样工作,但可以总结 .format

我的问题是如何更有效地使用 .format 方法。我尝试了 .format(switch) 和 .format([switch]) 但是你出错了。

避免再次写出所有变量的正确语法是什么

语法如下..

switch = [SW_1, Int_1, Vlan_num, Des]  #group of input variables
config1 = """
interface GigabitEthernet {1}
description {3}
switchport
switchport access vlan {2}
""".format(SW_1, Int_1, Vlan_num, Des)

print(config1)

尝试:

switch = [SW_1, Int_1, Vlan_num, Des] #group of input variables
config1 = """
interface GigabitEthernet {1}
description {3}
switchport
switchport access vlan {2}
""".format(*switch)

阅读 https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists 中有关使用单星的更多信息。如果您想进行更多研究,"unpacking arguments" 是您需要的关键词。