字符串(模板)使用中的默认值

2024-10-03 09:15:01 发布

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

希望您能帮助我解决下面的问题,我在python中遇到的问题。 我试图使用字符串格式的模板,但需要一些如何有默认值。在

template = """
Server {isServerRunning}
NTP {isNTPrunning}
Application {isAppRunning}
"""

test = template.format(isServerRunning='is not running')

print test

OUTPUT:
Server is not running
NTP is running
Application is running

(假设这3个参数的默认值应该是“正在运行”)

谢谢你的时间


Tags: 字符串test模板formatserverapplicationis格式
1条回答
网友
1楼 · 发布于 2024-10-03 09:15:01

你能把它包含在一个函数中,比如:

def test(isServerRunning='is running',
         isNTPrunning='is running',
         isAppRunning='is running'): 
         return template.format(
             isServerRunning=isServerRunning, 
             isNTPrunning=isNTPrunning, 
             isAppRunning=isAppRunning)

相关问题 更多 >