清除打印命令

2024-09-30 12:34:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用这行代码:

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')

我希望在脚本运行时,输出中不会出现括号和单引号。你知道吗

谢谢

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


Tags: the代码脚本apiforcallconfigurationvia
2条回答

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

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

下面是另一种使用格式打印的方法

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))

如果您使用的是python3,那么可以使用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

一种更具python风格的方法是在字符串上使用format函数

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

相关问题 更多 >

    热门问题