user1545352
user1545352

Reputation: 1

How to remove special chars in python

I have a python string which looks like this :

str='(/|\\)cmd\.exe$'

Now I want to remove the special characters and get string in following for,at:

new_str=replace_func(str)
print new_str
cmd.exe

Can someone help me , how to write this replace_func function. Thanks in advance!

Upvotes: 0

Views: 2348

Answers (2)

user695580
user695580

Reputation: 41

Use regex you can remove special characters

Here is a regex to match a string of characters that are not a letters or numbers:

[^A-Za-z0-9]+

ex:

import re
str='(/|\\)cmd\.exe$'
re.sub('[^A-Za-z0-9.]+', '',str)

Upvotes: 1

Cinder
Cinder

Reputation: 1609

try this:

string = "(/|\\)cmd\.exe$"

chars_to_remove = ("(|\\/$")
for char in chars_to_remove:
    string=string.replace(char, "")

print string

Upvotes: 0

Related Questions