首页 > 编程语言 > 详细

python字符串替换的2种方法

时间:2020-04-05 11:15:54      阅读:68      评论:0      收藏:0      [点我收藏+]

python字符串替换可以用两种方法实现:

1.用字符串本身的方法

2.用正则来替换字符串

下面用个例子来实验:

a = ‘hello word‘

我把a字符串里的word替换为python 

1.用字符串本身的replace方法

a.replace(‘word‘ , ‘python‘)

输出结果是hello  python

2.用正则表达式来完成替换:

import  re

strinfo = re . compile(‘word‘)

b = strinfo.sub(‘python‘,a)

printf (b)

输出结果也是hello  python

python字符串替换的2种方法

原文:https://www.cnblogs.com/cnn-ljc/p/12636227.html

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