如何使用python 2.7替换字符串上的当前单词?
这是我的字符串:如何使用python 2.7替换字符串上的当前单词?,python,string,python-2.7,Python,String,Python 2.7,这是我的字符串: marker = "AFK" replacement = "away from keyboard" line = "I will now go to sleep and be AFK until lunch time tomorrow." 我想替换我的行变量中“远离键盘”上的“AFK” 在Python2.7上最简单的方法是什么 谢谢。您可以像这样用replace替换(您还必须用replace返回的经过修改的字符串替换line,因为字符串是不可变的): 行=行。替换(标记,替换
marker = "AFK"
replacement = "away from keyboard"
line = "I will now go to sleep and be AFK until lunch time tomorrow."
我想替换我的行
变量中“远离键盘”上的“AFK”
在Python2.7上最简单的方法是什么
谢谢。您可以像这样用
replace
替换(您还必须用replace
返回的经过修改的字符串替换line
,因为字符串是不可变的):
<代码>行=行。替换(标记,替换)< /代码>这是一个答案:)可能希望在其中考虑单词边界。“notafkatthemotion”是否算作合适的替代品?
line = line.replace(marker, replacement)