Tania
Tania

Reputation: 1925

Removing backslashed substring from string

I have a list of substrings that needs to be removed from a string.

I have a blacklist of substrings that needs to be removed from the string when they occur in certain patterns.

Here's my code:

blacklist = ['bad','bye','hello']
mystring = "a\hello\hi\this_bye\end\my_bad.c"

for i in blacklist:
    mystring = mystring.replace("\\"+i,"").replace("_"+i,"")

But now this code returns mystring's value as

"a\\hello\\hi\\this_bye\\end\x08ad" 

instead of

"a\hi\this\end\my.c"

Can someone please tell me what is wrong? Thanks in advance.

Upvotes: 0

Views: 50

Answers (1)

user4097444
user4097444

Reputation:

It's working fine for me. I am using Python 2.7. As mentioned by Anand, please check which encoding you are using.

import sys
sys.getdefaultencoding()

Upvotes: 1

Related Questions