How to remove matching letters in a string that come after a comma? Python

Question:

I am working on a coding challenge that takes user input removes matching letters that come after a comma and scrubs the white space. For the life of me I can’t figure it out.

For example the user inputs:
Hello World, wea

Output would be:
Hllo orld

Any direction on how to solve this would be greatly appreciated it is driving me crazy.

Asked By: DaHawg

||

Answers:

Try using a simple for loop that iterates across the phrase and places characters that don’t appear after the comma into a separate string. Then the separate string is the result once the for loop has finished.

There are tons of different ways of achieving this, this way is fairly easy to understand though.

text = "Hello World, wea"
phrase, chars = text.split(",")  # split the text by the comma
chars = chars.strip()            # remove whitespace from second part
output = ""                      # separate string to collect chars
for letter in phrase:
    if letter.lower() not in chars: # check lowercased letters 
       output += letter             
print(output)

output

Hllo orld
Answered By: Alexander
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.