Kumar
Kumar

Reputation: 314

Remove extra characters in the string in Python

I have couple of strings (each string is a set of words) which has special characters in them. I know using strip() function, we can remove all occurrences of only one specific character from any string. Now, I would like to remove set of special characters (include !@#%&*()[]{}/?<> ) etc.

What is the best way you can get these unwanted characters removed from the strings.

in-str = "@John, It's a fantastic #week-end%, How about () you"

out-str = "John, It's a fantastic week-end, How about you"

Upvotes: 2

Views: 4786

Answers (4)

gariel
gariel

Reputation: 687

try

s = "@John, It's a fantastic #week-end%, How about () you"
chars = "!@#%&*()[]{}/?<>"
s_no_chars = "".join([k for k in s if k not in chars])
s_no_chars_spaces = " ".join([ d for d in "".join([k for k in s if k not in chars]).split(" ") if d])

Upvotes: 0

Dropout
Dropout

Reputation: 13866

Try out this:

import re

foo = 'a..!b...c???d;;'
chars = [',', '!', '.', ';', '?']

print re.sub('[%s]' % ''.join(chars), '', foo)

I presume that this is what you wanted.

Upvotes: 1

Jacek Przemieniecki
Jacek Przemieniecki

Reputation: 5967

import string

s = "@John, It's a fantastic #week-end%, How about () you"
for c in "!@#%&*()[]{}/?<>":
    s = string.replace(s, c, "")

print s

prints "John, It's a fantastic week-end, How about you"

Upvotes: 2

Dale Cooper
Dale Cooper

Reputation: 310

The strip function removes only leading and trailing characters. For your purpose I would use python set to store your characters, iterate over your input string and create new string from characters not present in the set. According to other stackoverflow article this should be efficient. At the end, just remove double spaces by clever " ".join(output_string.split()) construction.

char_set = set("!@#%&*()[]{}/?<>")
input_string = "@John, It's a fantastic #week-end%, How about () you"
output_string = ""

for i in range(0, len(input_string)):
    if not input_string[i] in char_set:
        output_string += input_string[i]

output_string = " ".join(output_string.split())
print output_string

Upvotes: 1

Related Questions