【问题标题】:python removing a string from another string using lists? [duplicate]python使用列表从另一个字符串中删除一个字符串? [复制]
【发布时间】:2020-06-14 08:34:49
【问题描述】:

我需要我的代码接受 2 个输入,一个字符串和第二个字符串。换句话说,第一个字符串是某种句子,第二个字符串只是要从整个句子中删除的几个字母。示例:

remove("This is as it is.", "is") == 'Th  as it .'

到目前为止,我有:

def remove(word, reword):
    s = set(reword)
    return (x for x in word if x not in s)

remove("This is as it is.", "is")
print(word)

【问题讨论】:

  • 使用.replace()

标签: python string list


【解决方案1】:

您可以为此使用替换功能。

def remove(word, reword):
    return word.replace(reword, '')

word = remove("This is as it is.", "is")
print(word)

【讨论】:

    猜你喜欢
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多