Sundararaman P
Sundararaman P

Reputation: 361

remove special character from string, not replace them with space

I'm trying to remove special characters from a string. All the examples available only replaces them with space. But i want to get rid of them and retain the order of the string. Below are some codes which i tried

input_string = "abcd. efgh ijk L.M"
re.sub(r"[^a-zA-Z0-9]+",'',input_string).split(' '))) #approach 1
re.sub(r"[\W_]+",'',input_string).split(' '))) #approach 2

The desired output is

"abcd efgh ijk LM"

Upvotes: 1

Views: 5674

Answers (5)

Ishalli Garg
Ishalli Garg

Reputation: 39

If you want to keep spaces then below regex will work like a charm

import re
input_string = "abcd. efgh ijk L.M"
re.sub('[^ A-Za-z0-9]+', '', input_string)

It will result this string - abcd efgh ijk LM

Upvotes: 0

atin
atin

Reputation: 893

You can add an space after the caret(^), like this

In [1]: re.sub(r"[^ a-zA-Z0-9]+",'',input_string)
#                  ^ space here
Out[1]: 'abcd efgh ijk LM'

If you also want to remove trailing or leading whitespaces you can use the strip method.

In [2]: '    Hello    '.strip()
Out[2]: 'Hello'

Upvotes: 1

jlesuffleur
jlesuffleur

Reputation: 1253

You can replace every special character (excluding space) by empty character:

re.sub(r"[^a-zA-Z0-9 ]+", '', input_string)

Upvotes: 1

Rumira Daksith
Rumira Daksith

Reputation: 194

# initializing bad_chars_list
bad_chars = [';', ':', '!', "*"]
 
# initializing test string 
test_string = "he;ll*o w!or;ld !"
 
# printing original string 
print ("Original String : " + test_string)
 
# using replace() to 
# remove bad_chars 
for i in bad_chars :
    test_string = test_string.replace(i, '')
 
# printing resultant string 
print ("Resultant list is : " + str(test_string))

Original String : "he;ll*o w!or;ld !"

Resultant list is : "hello world"

Upvotes: 0

Fredericka
Fredericka

Reputation: 304

You can use the .replace() function.

not_wanted_char = [".", "*"]

your_string = "abcd. efgh ijk L.M"

for i in not_wanted_char:
    your_string = your_string.replace(i,"")

Upvotes: 0

Related Questions