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.
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
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.
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