简体   繁体   中英

How to replace a current word on a string using python 2.7?

Here is my strings:

marker = "AFK"
replacement = "away from keyboard"
line = "I will now go to sleep and be AFK until lunch time tomorrow."

I want to replace "AFK" on "away from keyboard" in my line variable.

What the easiest way to do that on python 2.7?

Thanks.

您可以像这样用replace进行replace由于字符串是不可变的,因此您还必须用replace返回的更改后的字符串替换line ):

line = line.replace(marker, replacement)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM