Python中的字符串对象属性错误?

时间:2015-11-14 14:37:25

标签: python python-3.x

series = input("Please enter 10 numbers: ")
series.remove(5)

当我尝试删除所有出现的5时,我收到以下错误:

   line 2, in <module> series.remove(5)
AttributeError: 'str' object has no attribute 'remove'

什么是属性错误?为什么会这样?

1 个答案:

答案 0 :(得分:0)

字符串没有remove方法。如果要消除字符串中的每个匹配项,则应使用replace

series = input("Please enter 10 numbers: ")
series.replace('5','') #Here 5 is a string (substring of series you want to replace)

但他似乎更想要一个数字列表,更好的做法:

series = map(int, input("Please enter 10 numbers: ").split(separator))
series.remove(5) #Here is a number