-1

I've been trying for the last few hours to kick out some symbols out of a long string in a single shot but can't find any idea as to how I can remove them. If I go for using .replace() function it will be an uglier approach as the number of symbols is more than one and the function becomes excessively lengthier to cover them all. Any alternative way to remove them will be highly appreciated.

This is I tried:

exmpstr = "Hi there Sam! Don't you know that Alex (the programmer) created something useful or & easy to control"

print(exmpstr.replace("'","").replace("(","").replace(")","").replace("&",""))
print(exmpstr.replace("['()&]","")) #I know it can't be any valid approach but I tried

What I want to kick out are these symbols '()& from that string other than the way I tried with .replace() function.

SIM
  • 21,997
  • 5
  • 37
  • 109

3 Answers3

8

you can use a for-loop with replace:

for ch in "['()&]":
    exmpstr = exmpstr.replace(ch, '')

Or you could use a regular expression

import re
exmpstr = re.sub(r"[]['()&]", "", exmpstr)
nosklo
  • 217,122
  • 57
  • 293
  • 297
  • Thanks @nosklo for your solution. gonna accept it when time is right. – SIM Jul 12 '18 at 18:23
  • @RushabhMehta I don't agree that answering the questions hurt more than help. Feel free to close the question if you want. – nosklo Jul 12 '18 at 18:27
  • And, are you the one downvoting all of the other answers? And please read this: https://meta.stackoverflow.com/questions/276122/present-duplicates-i-answered-to – Rushabh Mehta Jul 12 '18 at 18:28
  • @RushabhMehta I have read that already. I just don't **agree** that the questions shouldn't be answered. Not everybody agrees with everybody here. That's why we have downvotes. I downvoted only one of the answers, the one I think is not helpful. – nosklo Jul 12 '18 at 18:31
1

It also does the trick:

exmpstr = "Hi there Sam! Don't you know that Alex (the programmer) created something useful or & easy to control"
expectedstr = ''.join(i for i in exmpstr if i not in "'()&")
print(expectedstr)
SIM
  • 21,997
  • 5
  • 37
  • 109
  • not evident in this simple example but sometimes this is the best approach! upvoted. You can remove the `[]` as there is no need to create an intermediate list: `''.join(i for i in exmpstr if i not in unwanted)` – nosklo Jul 12 '18 at 18:35
  • Thanks for the upvote @nosklo . Duly updated. – SIM Jul 12 '18 at 18:44
0

Actually, you are pretty close with your second attempt. Using regex sub for replacing, it can be done like this:

import re
regex = r"['()&]"

test_str = "\"Hi there Sam! Don't you know that Alex (the programmer) created something useful or & easy to control\""
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
    print (result)

If you want to replace the & with and run another:

result = re.sub(r" & ", " and ", test_str, 0, re.MULTILINE)

and remove the & from the first regex character group ['()&].

wp78de
  • 18,207
  • 7
  • 43
  • 71