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

python 替换字符串的方法replace()、正则re.sub()

时间:2019-09-04 11:52:55      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:字符串   正则   image   mic   compile   sub   字符   alt   code   

一、replace()函数
1用字符串本身的replace方法:

a = ‘hello word‘
b = a.replace(‘word‘,‘python‘)
print b
 
  • 1
  • 2
  • 3

二、re.sub()

import re
a = ‘hello word‘
strinfo = re.compile(‘word‘)
b = strinfo.sub(‘python‘,a)
print b

技术图片

python 替换字符串的方法replace()、正则re.sub()

标签:字符串   正则   image   mic   compile   sub   字符   alt   code   

原文地址:https://www.cnblogs.com/jfdwd/p/11457986.html

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