|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
如何在不用replace函数的情况下达到replace的功能?
- print(replace('abc def abc', 'def', 'abc')) # abc abc abc
- print(replace('abcabcabc', 'abc', 'hi')) # hihihi
- print(replace('This is out of control!', 'out of control!', 'better!')) # This is better!
复制代码
- def replace(string, old, new):
- while old in string:
- ind = string.index(old)
- string = string[:ind] + new +string[ind + len(old):]
- return string
- print(replace('abc def abc', 'def', 'abc')) # abc abc abc
- print(replace('abcabcabc', 'abc', 'hi')) # hihihi
- print(replace('This is out of control!', 'out of control!', 'better!')) # This is better!
复制代码
|
|