python字符串的replace方法替换目标穿中的特定字符

类似于word中的替换操作,我们可以用replace来把一个字符串中的某些字符用新的字符替换,类似于英文replace a with b的语法,具体的代码如下:

s = "I love python!"
sr = s.replace('o','O')
print(sr)

运行结果如下:

I lOve pythOn!
[Finished in 0.6s]

我们看到,我们replace 了o with O。

发布了152 篇原创文章 · 获赞 6 · 访问量 4029

猜你喜欢

转载自blog.csdn.net/weixin_41855010/article/details/104436656
今日推荐