replace()替换,相当于文本替换功能;
语法
字符串序列.replace(旧的子串,新的子串,替换次数)
使用.replace()代码
实现将字符串中的and替换成or
替换1次,从左到右执行
mystr="hello world and itcast and itheima and python"
print(mystr.replace('and','or',1))
执行结果:
hello world or itcast and itheima and python
全部替换,次数就不需要填写
mystr="hello world and itcast and itheima and python"
print(mystr.replace('and','or'))
执行结果:
hello world or itcast or itheima or python