首页 > 编程语言 > 详细

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

时间:2014-09-03 16:26:46      阅读:349      评论:0      收藏:0      [点我收藏+]

 替换字符串中的子串


 任务:

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

 

解决方案:

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

  

 

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

原文:http://www.cnblogs.com/wuzhiming/p/3944180.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!