Python:使用用户输入。我下面的代码只能从字符串中去掉一个字符。我需要它能够剥离多个字符

Python:使用用户输入。我下面的代码只能从字符串中去掉一个字符。我需要它能够剥离多个字符,python,string,Python,String,您可以检查单词中的每个字符,然后使用str.join将未删除的字符连接在一起。例如: word = input("Enter a string/sentence: \n") char = input("\nWhich characters would you like to make disappear? \n") new_word = word.replace(char, "") print("\n" + str(

您可以检查单词中的每个字符,然后使用
str.join
将未删除的字符连接在一起。例如:

word = input("Enter a string/sentence: \n")
char = input("\nWhich characters would you like to make disappear? \n")
new_word = word.replace(char, "")
print("\n" + str(new_word))
word = input("Enter a string/sentence: \n")
chars = input("\nWhich characters would you like to make disappear? \n")
new_word = "".join(ch for ch in word if ch not in chars)    # <-- use str.join
print("\n" + str(new_word))