首页 > 解决方案 > 如何在python中替换短语中的字符串?

问题描述

所以这是我在pyhton中制作的代码,由于某种原因它不起作用所以我希望这里的人可以帮助我找到解决方案*我只是一个初学者*

    def replace(phrase):
    replaced = ""
    for word in phrase:
        if word == ("what"):
            replaced = replaced + "the"
        else:
            replaced = replaced + word
    return replaced

    print(replace(input("enter a phrase: ")))

标签: pythonpython-3.x

解决方案


请尝试替换方法:

def replace(phrase):
  replaced = phrase.replace("what","the")
  return replaced

推荐阅读