Python3 - 替换字符串require if语句?

时间:2014-05-21 18:36:44

标签: python python-3.4

我只想用任何东西替换'dog'

认为我需要一个if语句,但不能得到一个有意义的语句:

wordlist = ['cat','dog','rabbit','test',]

letterlist = []
for aword in wordlist:
    aword.replace('dog', 'OMG IT WORKED? OR NOT')
    print (aword)
    letterlist.append(aword)

print(letterlist)

1 个答案:

答案 0 :(得分:1)

str.replace() 返回一个新字符串;你想重新aword到它:

aword = aword.replace('dog', 'OMG IT WORKED? OR NOT')

str是一个不可变类型;您只能通过更改生成新值,而不是更改值本身。