欢迎您 本站地址:  
首页   编程教学  

Python3 replace()方法

Python3 字符串 Python3 字符串


描述

replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。

语法

replace()方法语法:

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

参数

返回值

返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次。

实例

以下实例展示了replace()函数的使用方法:

实例

#!/usr/bin/python3 str = "www.w3cschool.cc" print ("空若网旧地址:", str) print ("空若网新地址:", str.replace("w3cschool.cc", "www.kongruo.cn")) str = "this is string example....wow!!!" print (str.replace("is", "was", 3))

以上实例输出结果如下:

空若网旧地址: www.w3cschool.cc
空若网新地址: www.www.kongruo.cn
thwas was string example....wow!!!

Python3 字符串 Python3 字符串

温馨提示

扫描下方二维码,访问手机版。