Skip to main content
 首页 » 编程设计

python之清理打印命令

2025年05月04日21落叶无声

我正在使用这行代码:

fwhost1 = "172.16.17.1" 
print("Connecting via API call, backing up the configuration for:", fwhost1) 

这一行的输出是:

('Connecting via API call, backing up the configuration for:', '172.16.17.1') 

我希望在脚本运行时不要在输出中出现括号和单引号。

谢谢

我试过调整代码行,但这是唯一不会出错的方法

请您参考如下方法:

您可以使用+ 运算符连接字符串。更多信息 here

fwhost1 = "172.16.17.1"  
print("Connecting via API call, backing up the configuration for: " + fwhost1) 

这是使用 %-formatting 打印的另一种方式

print("Connecting via API call, backing up the configuration for: %s" % fwhost1) 

另一种选择是使用 str.format()

print("Connecting via API call, backing up the configuration for: {}".format(fwhost1)) 

如果您使用的是 Python 3,则可以使用 f-strings

print(f"Connecting via API call, backing up the configuration for: {fwhost1}") 

输出

Connecting via API call, backing up the configuration for: 172.16.17.1