-2

here is my code:

string = input("Enter a string:")
print(string)

characters = input("Enter the characters you would like to make disappear:")
print(characters)

print(string.replace("characters"," "))

I need to display variable string without the entered characters in variable characters

SuperStew
  • 2,857
  • 2
  • 15
  • 27
Ophethile
  • 21
  • 6
  • 1
    i think you'll need to loop through each letter you want to disappear and replace it – SuperStew Apr 06 '22 at 14:10
  • 1
    Make characters a list, then iterate over that list and call `string.replace(listitem, "")` on each one. Heck, you don't even need to make it a list first, just iterate over `characters` char by char. – MattDMo Apr 06 '22 at 14:10
  • 2
    Don't use `string` as name, it's module from standard library – buran Apr 06 '22 at 14:13
  • @buran As long as they are not using the string module, i think its fine to use `string` as a variable name. – Billy Apr 06 '22 at 14:14
  • 1
    @Billy, it's fine until it's not and bites them. Better learn not to commit such errors. SO is full of questions about errors due to name collisions with built-in modules. – buran Apr 06 '22 at 14:16
  • @MattDMo, I just looped through each letter and replaced it, but now what the program does is replace them individually line after line and I want it to replace all the letters in a single line. – Ophethile Apr 06 '22 at 18:53
  • Please [edit] your post and add the code you're currently using, as well as the input, the output you're getting, and the output you want to get. – MattDMo Apr 07 '22 at 14:23

2 Answers2

0

You need to iterate over the characters, and replace each one by one

Example

for ch in "characters":
    string.replace(ch, "")
Billy
  • 1,157
  • 1
  • 9
  • 18
0

Example using re to remove all vowels in your string:

>>> import re
>>> characters_to_remove = "aeiouy"
>>> my_string = "my string"
>>> re.sub(f"[{characters_to_remove}]", "", my_string)
'm strng'
bonnal-enzo
  • 1,165
  • 9
  • 19