现在的位置: 首页 > 综合 > 正文

001_017 Python 替换字符串中的子串string.template

2018年02月15日 ⁄ 综合 ⁄ 共 274字 ⁄ 字号 评论关闭

代码如下:

#encoding=utf-8
print '中国'

#根据字典内容,替换被标记的字符串

import string

new_style=string.Template('this is $thing')

print new_style.substitute({'thing':5})
print new_style.substitute({'thing':'test some'})


old_style='this is %(thing)s'
print old_style%{'thing':5}

打印结果如下:

中国
this is 5
this is test some
this is 5

抱歉!评论已关闭.