0

I want to replace the following string with an empty string.

I cannot type in my inputs here, for some reason, those symbols are ignored here. Kindly look at the image below. My code produces weird results. Kindly help me out here.

#expected output is "A B C D E"

string = "A<font color=#00FF00> B<font color=#00FFFF> C<font color="#00ff00"> D<font color="#ff0000"> E<i>"

lst = ['<i>','<font color=#00FF00>','<font color=#00FFFF>','<font color="#00ff00">','<font color="#ff0000">']

for el in lst:
    string.replace(el,"")
print string
alexwlchan
  • 5,699
  • 7
  • 38
  • 49
brain storm
  • 30,124
  • 69
  • 225
  • 393

2 Answers2

2

In python strings are immutable, i.e doing any operation on a string always returns a new string object and leaves the original string object unchanged.

Example:

In [57]: strs="A*B#C$D"

In [58]: lst=['*','#','$']

In [59]: for el in lst:
   ....:     strs=strs.replace(el,"")  # replace the original string with the
                                       # the new string

In [60]: strs
Out[60]: 'ABCD'
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
0
>>> import string
>>> s="A*B#C$D"
>>> a = string.maketrans("", "")
>>> s.translate(a, "*#$")
'ABCD'
Rakesh
  • 81,458
  • 17
  • 76
  • 113