0

For homework I am trying to strip a set of characters inputted from the user from a sentence inputted by a user. This is the code that I have come up with so far using Python 2.7:

    sentence = raw_input("Please write a sentence. ")
    remove = raw_input("Please input characters to remove from sentence. ")

    remove = remove.strip(" ") # in case user added spaces
    sentence = sentence.strip(remove)
    print sentence
  • Have you thought about utilizing a for loop to remove characters one at a time? You could also use the regex module (`re.sub`, for example) as another solution – rahlf23 Jun 18 '18 at 18:06
  • `str.strip()` only removes the characters from the start or end of the string. – pault Jun 18 '18 at 18:07
  • 3
    Possible duplicate of [Remove specific characters from a string in Python](https://stackoverflow.com/questions/3939361/remove-specific-characters-from-a-string-in-python) – Grant Williams Jun 18 '18 at 18:08
  • I could try using a loop, perhaps that can work for me. we have not learnt regex module yet, so that may be outside of the scope of the lesson. – Pierre Roodman Jun 18 '18 at 18:15
  • I do not see the similarity between what I am doing and the possible duplicate @GrantWilliams – Pierre Roodman Jun 18 '18 at 18:17
  • @PierreRoodman from the answer selected in the possible duplicate `removed = re.sub('[!@#$]', '', sentence)` you'd have to update the character class to match your input `remove` variable though – Grant Williams Jun 18 '18 at 19:20
  • Thank you @GrantWilliams I will definitely research on the regex module – Pierre Roodman Jun 19 '18 at 20:36

4 Answers4

0

I would suggest using a regex and leveraging character class as follows:

import re
sentence = raw_input("Please write a sentence. ")
remove = raw_input("Please input characters to remove from sentence. ")
regex = "[" + re.escape(remove) + "]"
sentence = re.sub(regex, "", sentence)
print(sentence)

It takes the user input and escapes anything that is a special regex character. That way if a user puts in .* it won't have unexpected results. Then it substitutes any of those characters with "" which is nothing.

sniperd
  • 5,124
  • 6
  • 28
  • 44
0

Use sentence.replace(remove, "") to replace specific characters with an empty string, or in other words, remove a specific string.

In your code, sentence.strip(remove) didnt work because it only removes words from the front and the end of the of the string.

0

as HUAN said in his answer, sentence.strip will only remove characters from the beginning and the end of the sentence string, to remove letters in between you should use sentence.replace, here's your code I rewrote to use replace, I also used a for loop to remove multiple characters, so if the user input something like "hw" the sentence "hello world" becomes "ello orld":

sentence = raw_input("Please write a sentence. ")
remove = raw_input("Please input characters to remove from sentence. ")

remove = remove.strip(" ") # in case user added spaces
for c in remove:
    sentence = sentence.replace(c,"")
print sentence
Iyad Ahmed
  • 80
  • 2
  • 12
  • can you explain how "c in remove" works I posted an answer already for the a while loop that I used. – Pierre Roodman Jun 18 '18 at 18:43
  • c is just a variable, you can name it anything else, on each loop the c variable stores a character of the string remove, python supports string indexing so we can treat it as an iterable,so we can iterate through it just like a list, so in the first loop c stores the first character of the string, and the next loop, c stores the second character in the string, and so on until there are no more characters. – Iyad Ahmed Jun 18 '18 at 22:13
  • Syntax. for iterating_var in sequence: statements(s) – Iyad Ahmed Jun 18 '18 at 22:16
  • Thank you @Redivider , this was very informative. Much simpler method. – Pierre Roodman Jun 19 '18 at 20:29
  • You're Welcome. – Iyad Ahmed Jun 19 '18 at 22:27
0

This is what worked for me had to use a loop:

   sentence = raw_input("Please write a sentence. ")
   remove = raw_input("Please input characters to remove from sentence. ")

   remove = remove.replace(" ", "")
   i = 0
   while i < len(remove):
       sentence = sentence.replace(remove[i], "")
       i += 1
   print sentence