Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3-替换字符串require if语句?_Python_Python 3.4 - Fatal编程技术网

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

Python3-替换字符串require if语句?,python,python-3.4,Python,Python 3.4,我只想用任何东西来代替“狗” 我想我需要一个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) str.repla

我只想用任何东西来代替
“狗”

我想我需要一个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)
str.replace()
返回一个新字符串;您想将
aword
重新绑定到它:

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

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

该死的Martjin,我终于可以回答这个问题了。你的速度太快了。