replace
   
   不是就地方法,而是返回新字符串,因此需要将结果分配给新字符串。
  
  
   从文档中:
   
    https://docs.python.org/3/library/stdtypes.html#str.replace
   
  
  
   
    str.replace(旧的,新的[,计数])
    
    返回字符串的副本,所有出现的子字符串old替换为new。如果给定了可选参数计数,则只替换出现的第一个计数。
   
  
  
   如果同时迭代键和值,您的逻辑也可以简化很多,如下所示
  
  def replace_exception_chars(string):
    exception_chars_dict = {'Old': 'New', 'old': 'new'}
    #Iterate over key and value together
    for key, value in exception_chars_dict.items():
        #If key is found, replace key with value and assign to new string
        if key in string:
            string = string.replace(key, value)
    return string
print(replace_exception_chars('Old, not old'))
  
   输出将是
  
  New, not new