2024-10-06 20:35:06 发布
网友
我只想替换一个字符串中的特定单词。然而,其他一些词里面有这个词,但我不希望它们被改变
例如,对于下面的字符串,我只想用z字符串中的y替换x。怎么做
z
y
x
x = "the" y = "a" z = "This is the thermometer"
在您的情况下,可以使用re.sub(x, y, z)。 有关详细信息,请阅读文档here
re.sub(x, y, z)
import re pattern=r'\bthe\b' # \b - start and end of the word repl='a' string = 'This is the thermometer' string=re.sub(pattern, repl, string)
在您的情况下,可以使用
re.sub(x, y, z)
。 有关详细信息,请阅读文档here相关问题 更多 >
编程相关推荐