《Python CookBook2》 第一章 文本 - 替换字符串中的子串

 替换字符串中的子串


 任务:

  给定一个字符串,通过查询一个字符串替换字典,将字符串中被标记的子字符串替换掉。

 

解决方案:

>>> 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

  

 

posted @ 2014-09-03 15:27  道生一_三生万物  阅读(212)  评论(0编辑  收藏  举报
returnTop $(function(){ $('#returnTop').click(function () { $('html,body').animate({ scrollTop: '0px' }, 800); returnfalse; }); });