Py学习  »  Python

为什么replace()在我的Python函数中不起作用?[副本]

Quanti Monati • 5 年前 • 1884 次点击  

以下是实际代码:

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'))

如果我试图运行它,我将在输出中获得未更改的源字符串。请看一下: enter image description here

为什么会这样?

更新 期望输出:

新的,不是新的

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51338
 
1884 次点击  
文章 [ 6 ]  |  最新文章 5 年前