码迷,mamicode.com
首页 > 编程语言 > 详细

用python实现字符串的替换

时间:2016-03-04 16:06:00      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:


‘‘‘
    字符串替换两种方法:
    1、字符串本身的replace方法
    2、用正则表达式完成替换

‘‘‘
import re


if __name__ == ‘__main__‘:
    str1=str2 = "hello world,my my  python world!"
    #1
    print str1.replace("world", "hahahaha")
    #2
    strinfo = re.compile(‘world‘)
    b = strinfo.sub(‘hahahaha‘,str2)
    print b 

 

结果:

hello hahahaha,my my  python hahahaha!
hello hahahaha,my my  python hahahaha!

 

用python实现字符串的替换

标签:

原文地址:http://www.cnblogs.com/luckycongcong/p/5242237.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!