【问题标题】:Python replace function nor working as expected [duplicate]Python替换功能无法按预期工作[重复]
【发布时间】:2020-06-15 12:01:47
【问题描述】:

对于下面的代码,我不会替换旧 txt 中的任何内容。我期待它返回:“我的元音去哪儿了?”

def uncensor(txt, vowels):
    c = 0
    for a in txt:
        if a == '*':
            txt.replace('*', vowels[c], 1)
            c=c+1
    return txt

uncensor("Wh*r* d*d my v*w*ls g*?", "eeioeo")

但是,如果我这样做,我可以替换字母:

txt = 'Wh*r* d*d my v*w*ls g*?'
vowels = 'eeioeo'
txt.replace('*', vowels[0],1)

我哪里做错了?

【问题讨论】:

  • 需要使用返回值txt = txt.replace('*', vowels[c], 1)

标签: python


【解决方案1】:

你需要返回结果:

result = txt.replace('*', vowels[c], 1)

【讨论】:

    猜你喜欢
    • 2016-11-12
    • 2021-10-13
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多