-4

I'm teaching myself Python and I have been stuck on this issue for a few days now.

The idea is to ask a user to input a sentence and then ask them for 5 characters that they would like to remove from the sentence.

For example the sentence input by the user is: user_string = "The quick brown fox jumps over the lazy dog"

The characters they want to remove is: lst = ["a", "b", "c", "d", "e"]

I have reached the point where I have the user string and the user list that needs to be removed, what I'm stuck on is figuring out how to loop through the list and check if each character is present in the string and then to strip it from the string.

I have tried to use a for loop but I'm not yet proficient in loops so I might be going about it the wrong way, this is my for loop so far:

for char in user_string[:]:
    if char[0] in user_string:
        removed_string = user_string.strip(char)
print(removed_string)
  • Can you share a full working example ? – azro Nov 20 '22 at 08:55
  • 1
    the short answer is that you can't. Strings are immutable, therefore you cannot add or remove characters from them once they have been created. All of the methods you use to modify strings are actually creating a new string with specified modifications. but the original string remains the same. – Alexander Nov 20 '22 at 09:00
  • 1
    Does this answer your question? [Python strip() multiple characters?](https://stackoverflow.com/q/3900054/6045800) – Tomerikoo Nov 20 '22 at 09:03
  • `removed = set( ["a", "b", "c", "d", "e"]), sol = ''.join(i for i in user_stirng if i not in removed)` – sahasrara62 Nov 20 '22 at 09:04
  • this is well defined question with a proper example, why to close this ? – sahasrara62 Nov 20 '22 at 09:05
  • Because it's a very common duplicate? @sahasrara62 Your answer provided in a comment is already [here](https://stackoverflow.com/a/3900078/6045800) – Tomerikoo Nov 20 '22 at 09:07
  • @Tomerikoo yes it is just a common problem, but the tag under which it is closed, i refered to it. if it is marked as duplicate then it would be great – sahasrara62 Nov 20 '22 at 09:24

2 Answers2

1

The method user_string.strip(char) only removes leading and padding char, so you can't call it agin and again on the initial char

Here's 2 ways :

user_string = "The quick brown fox jumps over the lazy dog"
lst = ["a", "b", "c", "d", "e"]

# either collect valid chars
result = ""
for c in user_string:
    if c not in lst:
        result += c
print(result)

# either remove invalid chars
result = user_string[:]
for to_remove in lst:
    result = result.replace(to_remove, "")
print(result)
azro
  • 53,056
  • 7
  • 34
  • 70
0

Iterate over the list of user input characters and replace their occurrence in the string with a null character ""

user_string = "the quick brown fox jumps over the lazy dog"

chars_to_remove = ["a", "b", "c", "d", "e"]

for char in chars_to_remove:
    user_string = user_string.replace(char, "")

print(user_string)

Output:

th quik rown fox jumps ovr th lzy of
Geeky Quentin
  • 2,469
  • 2
  • 7
  • 28