【发布时间】:2020-01-23 15:22:10
【问题描述】:
-
示例 1:
"fogfogfffoooofftreesggfoogfog"-- 在此示例中,我需要删除'fogFOG',我应该得到输出为'trees'。 -
示例 2:
"fogFogFogffffooobirdsandthebeesGGGfogFog"-- 输出应该类似于'birdsandthebees'
str="fogFogFogffffooobirdsandthebeesGGGfogFog"
str1="fogFOG"
for i in str1:
Str=str.replace(i,'')
print(Str)
Str=str.replace(i) 出现错误
TypeError: replace() takes at least 2 arguments (1 given)
如果我在列表中单独使用所有字母,我会得到输出。但我想知道我们如何使用单个字符串来做到这一点。
【问题讨论】:
-
确实,
replacetakes at least 2 arguments,它应该在str的实例上调用,而不是str类本身。 -
你应该首先命名你的变量,而不是使用像 str 这样的保留字
-
你的问题代码中没有
Str=str.replace(i)这一行,那么错误是从哪里来的呢? -
请检查您的代码是否与您发布的相同,您得到的错误似乎是您的代码的另一个版本。
-
通过使用保留的python 类型
str,您已经重新定义了它并且可能破坏了所有字符串操作函数。更改您的对象名称,使其不会与 Python 内置类型冲突。
标签: python python-3.x list python-2.7 python-requests