这是实际代码:
def replace_exception_chars(string):
exception_chars_dict = {'Old': 'New', 'old': 'new'}
exception_chars_keys = list(exception_chars_dict.keys())
for exception_char in exception_chars_keys:
if exception_char in string:
string.replace(exception_char, exception_chars_dict[exception_char])
return string
print(replace_exception_chars('Old, not old'))
If I'm trying to run it I'm getting unchanged source string in OUTPUT. Please have a look:
为什么会这样呢?
更新
期望的输出:
新的,不新的
replace()
不在原地工作:
方法replace()返回字符串的副本,其中出现的旧字符串已替换为新字符串,可选择将替换数量限制为最大。
所以你错过了作业:
string = string.replace(exception_char, exception_chars_dict[exception_char])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)