python - 如何在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: ")))
解决方案
请尝试替换方法:
def replace(phrase):
replaced = phrase.replace("what","the")
return replaced
推荐阅读
- ios - 将完整的编程自定义视图添加到 ViewController。自动布局问题
- python - 400 错误请求:KeyError:“文件”
- python - 如何通过 selenium 仅针对 Python 中的当前 Chrome 会话按下键盘按钮
- chocolatey - Chocolatey 问题 - 创建 Powershell 脚本的 zip 包?
- python - 如何将 PDF 转换为 opencv-python 可读的图像?
- node.js - Telegram 机器人可以在单个消息触发器上返回多条消息吗?
- javascript - 我正在尝试编写一个程序,其中球忽略击中绿色障碍,但当红色障碍表示游戏结束时结束游戏
- regex - 提取斜线之间的字符串,但第二个斜线可能不存在
- r - 考虑了嵌套 ifelse 中的一些条件
- c# - 如何在不缩放纹理的情况下实现缩放行为?