python——replace函数

replace()方法返回当前old换成new,可选择的替代限制到最大数量的字符串的副本。

语法:
str.replace(old, new[, max])

参数
old – 这是要进行更换的旧子串。
new – 这是新的子串,将取代旧的子字符串(子串可以为空)。
max – 如果这个可选参数max值给出,仅第一计数出现被替换。
返回值
此方法返回字符串的拷贝与旧子串出现的所有被新的所取代。如果可选参数最大值给定,只有第一个计数发生替换。
例子1

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

例子2

n = input()
n = n.lower()
n = n.replace("a", "")
n = n.replace("e", "")
n = n.replace("i", "")
n = n.replace("o", "")
n = n.replace("u", "")
n = n.replace("y", "")
print (n)

结果:将输入的a,e,i,o,u,y全部删除

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值