2014-03-25 39 views
1

希望你能帮我解决我在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个paramters默认值应该是 '运行')

感谢您的时间

回答

0

你能包住,在像函数:

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