要替换某个词在一个字符串,但不断收到followinf结果:替换命令条件替换字符串
字符串:"This is my sentence."
在他们想要的用户类型来代替:"is"
"was"
新的字符串:"Thwas was my sentence."
我怎样才能确保它只替换词“是”,而不是它找到的任何字符串?
代码功能:
import string
def replace(word, new_word):
new_file = string.replace(word, new_word[1])
return new_file
任何帮助非常感谢,谢谢!
好找。我搜索了一个重复但用“替换”术语,而不是“查找”。 –
所以没有I:[蟒替换的词不部分单词](https://www.google.ca/search?client=safari&rls=en&q=python+replace+words+not+partial+words&oq=python+replace+words+未+部分+词语gs_l = serp.3 ... 44021.51387.0.51523.33.30.0.3.3.0.127.2123.23j5.28.0 .... 0 ... ... 1c.1.64.serp..2.18.1097 0j0i67k1j0i131k1j0i22i30k1j33i21k1。 0mLcUwAiDdk)。我认为这里的关键词“不是部分词”是关键。 –
至少现在的“替换”部分被覆盖:) –