python - 属性错误: 'str' object has no attribute 'remove' ,如何修复?

标签 python python-3.x

我创建了一个返回以下错误的函数:

original_alphabet.remove(value)
AttributeError: 'str' object has no attribute 'remove'

我不知道如何修复该错误,感谢任何帮助。这是我的代码:

def keyword_cipher_alphabet(keyword):
 original_alphabet = "abcdefghijklmnopqrstuvwxyz"

 for value in original_alphabet:
  if value in keyword:
   original_alphabet.remove(value)

 keyword_alphabet = ""
 for value in original_alphabet:
  keyword_alphabet += value 

 user_keyword = ""
 for value in keyword:
  user_keyword += value

 result = user_keyword + keyword_alphabet

 return result.upper()

最佳答案

导致该错误的原因是字符串没有方法remove。您可以尝试替换:

$> my_str = 'abc'
$> my_str = my_str.replace('b', '')
$> my_str
'ac'

关于python - 属性错误: 'str' object has no attribute 'remove' ,如何修复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47320725/

相关文章:

python - 如何在 Python 中按特定数字自动递增重复值?

python - Python 中是否有允许在单个文件中管理虚拟文件系统的库?

python - Namedtuple '_source' 方法在使用 'exec' 引用后停用

python - 在 Snow Leopard 上安装 MySQLdb

python - 修复 Python 的 Pg 中的类型错误

Python zipfile 通过 suds 发送错误 : "' ascii' codec can't decode byte 0x8c in position 10: ordinal not in range(128)"

python - 使用 pyev 进行 epool 还是从 Python 中的 stdlib 中选择?

python - 如何将零轴移动到python中的最后一个位置?

python - 在 Ubuntu 中为 weasyprint 安装最新的 cairo lib

Python 3 - 计算两个列表中的匹配项(包括重复项)