用python实现字符串的替换


'''
    字符串替换两种方法:
    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!

 

posted @ 2016-03-04 15:24  小木桐梧  阅读(533)  评论(0编辑  收藏  举报