python怎么替换字符串的内容-Python教程

资源魔 26 0

Python中replace()函数把字符串中的 old(旧字符串) 交换成 new(新字符串),假如指定第三个参数max,则交换没有超越 max 次。

replace()函数语法:

str.replace(old, new[, max])

参数:old -- 将被交换的子字符串。new -- 新字符串,用于交换old子字符串。max -- 可选字符串, 交换没有超越 max 次。

前往值:前往字符串中的 old(旧字符串) 交换成 new(新字符串)后天生的新字符串,假如指定第三个参数max,则交换没有超越 max 次。

如下实例展现了replace()函数的应用办法:

实例:

#!/usr/bin/python
str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);

以上实例输入后果以下:

thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string

更多Python相干技巧文章,请拜访Python教程栏目进行学习!

以上就是python怎样交换字符串的内容的具体内容,更多请存眷资源魔其它相干文章!

标签: python教程 python编程 python使用问题 python替换字符串的内容

抱歉,评论功能暂时关闭!