gpt4 book ai didi

python - 替换字符串中的句点 (".")

转载 作者:行者123 更新时间:2023-12-03 23:17:29 25 4
gpt4 key购买 nike

我有一个类型为 ../sometext/someothertext 的字符串,我正在尝试替换 ..在带有网站名称的字符串中 http://www.website.com .

在 Python 中,我所做的是这样的:

strName = "../sometext/someothertext"
strName.replace("..", "http://www.website.com")
print strName

但我得到的唯一输出是
../sometext/someothertext

我也试过逃避句号,比如
strName = ../sometext/someothertext
strName.replace("\.\.", "http://www.website.com")

但输出不会改变。我该怎么做呢?

最佳答案

你没有分配结果......

strName = strName.replace("..", "http://www.website.com")
.replace不修改原始字符串,而是返回一个带有修改的新字符串。

关于python - 替换字符串中的句点 ("."),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14750942/

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