替换字符串中的句点(“.”)

2024-09-30 02:15:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类型为../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")

但产量没有变化。我该怎么做?


Tags: 字符串名称comhttp类型网站wwwwebsite
2条回答

你没有分配结果。。。

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

.replace不修改原始字符串,但返回一个带有修改的新字符串。

最好指定一个替代品,以防有其他替代品。。在里面

strName = strName.replace("..", "http://www.website.com", 1)

相关问题 更多 >

    热门问题