python 字符串输出方式

方法1:模版方式

Template类继承了string类

$()与$后面添加的是变量

$$显示$字符

>>> from string import Template
>>> t = Template('${village}folk send $$10 to $cause.')
>>> t.substitute(village='Nottingham', cause='the ditch fund')
'Nottinghamfolk send $10 to the ditch fund.'

注意:substitue()方法异常,产生KeyError,但是用safe_substitue()方法更加合适,因为这个方法可以防止输入参数缺少时,产生的异常情况,默认是显示变量本省

 

 
posted @ 2015-04-22 10:18  李自泰  阅读(242)  评论(0编辑  收藏  举报