# coding=gbk
guests = ['apple', 'boy', 'cat', 'dog']
print(guests)
guests_update = guests.replace('apple','egg')
print(guests_update)
报错:AttributeError: 'list' object has no attribute 'replace'
解决:
guests = ['apple', 'boy', 'cat', 'dog']
guests = str(guests)
append报错
# coding=gbk
guests = ['apple', 'boy', 'cat', 'dog']
guests = str(guests)
print(guests)
guests_update = guests.replace('apple','egg')
print(guests_update)
guests_update.append('frog')
print(guests_update)
报错:AttributeError: ‘str‘ object has no attribute ‘append‘
解释:Of course not, because it's a string and you can't append to string. String are immutable.
解决:在str()前append
# coding=gbk
guests = ['apple', 'boy', 'cat', 'dog']
guests.append('frog')
print(guests)