替换字符串中的子串
任务:
给定一个字符串,通过查询一个字符串替换字典,将字符串中被标记的子字符串替换掉。
解决方案:
>>> import string>>> new_style = string.Template('this is $thing')#给substitute 方法传入一个字典参数并调用>>> print new_style.substitute({ 'thing':5})this is 5>>> print new_style.substitute({ 'thing':'test'})this is test#关键字参数,并调用>>> print new_style.substitute(thing = 5)this is 5>>> print new_style.substitute(thing = 'test')this is test