gpt4 book ai didi

python - 字符串替换不起作用

转载 作者:行者123 更新时间:2023-11-28 19:55:10 27 4
gpt4 key购买 nike

有点不好意思问:

但是为什么这不能成功地工作呢? .statswith 方法在这里很好,返回 True,但我无法让这个替换工作...

sandwich = "Au Cheval Cheeseburger"
restaurant = "Au Cheval"
if sandwich.startswith(restaurant):
sandwich.replace(restaurant, "")
print sandwich

(如果你不知道,我想让三明治只读芝士汉堡。还有比通过字符串替换更有效的方法吗?

谢谢大家

编辑:上帝啊,我就知道这很愚蠢。已经很晚了,脑子都炸了。谢谢大家。

最佳答案

来自 http://www.tutorialspoint.com/python/string_replace.htm

The method replace() returns a copy of the string in which the occurrences of old have been replaced with new

尝试sandwich = sandwich.replace(restaurant, "")

关于python - 字符串替换不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30340833/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com