Sunday, September 7, 2025
HomeLanguagesPython Program to Remove Special Characters From String

Python Program to Remove Special Characters From String

Remove special characters from string in python; In this tutorial, You will learn how to remove special characters from string in python.

A special character is one that is not considered a number or letter. Symbols, accent marks, and punctuation marks are considered special characters. Similarly, ASCII control characters and formatting characters like paragraph marks are also special characters.

How to Remove Special Characters From String in Python

  • 1: Remove special characters from string in python using replace()
  • 2: Remove special characters from string in python using  join() + generator
  • 3: Remove special characters from string in python using  Using filter()

1: Remove special characters from string in python using replace()

In the below python program, we will use replace() inside a loop to check special characters and remove it using replace() function.

# Python code to remove special char
# using replace() 
  
# initializing special characters 
sp_chars = [';', ':', '!', "*"] 
  
# given string  
givenStr = "Hel;lo *w:o!r;ld * de*ar !"
  
# print given string
print ("Original String : " + givenStr) 
  
# using replace() to  
# remove special chars  
for i in sp_chars : 
    givenStr = givenStr.replace(i, '') 
  
# printing resultant string  
print ("After Remove special char : " + str(givenStr)) 

After executing the program, the output will be:

Original String : Hel;lo *w:o!r;ld * de*ar !
After Remove special char : Hello world  dear

2: Remove special characters from string in python using  join() + generator

In the below python program, we will use  join()  to remove special characters from a given string. And create a new string in python.

# Python code to remove special char
# using join() + generator 
  
# initializing special characters 
sp_chars = [';', ':', '!', "*"] 
  
# given string  
givenStr = "Hel;lo *w:o!r;ld * de*ar !"
  
# print given string
print ("Original String : " + givenStr) 
  
# using join() + generator to  
# remove special chars  
givenStr = ''.join(i for i in givenStr if not i in sp_chars) 
  
# printing resultant string  
print ("After Remove special char : " + str(givenStr)) 

.banner-1-multi-360{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:auto!important;margin-right:auto!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:250px;padding:0;text-align:center!important;width:100%}

After executing the program, the output will be:

Original String : Hel;lo *w:o!r;ld * de*ar !
After Remove special char : Hello world  dear

3: Remove special characters from string in python using  Using filter()

This is yet another solution to perform remove special characters from string. Using the lambda function with filter function can remove all the special characters from a string and return new string without special characters.

# Python code to remove special char
# using filter() 
  
# initializing special characters 
sp_chars = [';', ':', '!', "*"] 
  
# given string  
givenStr = "Hel;lo *w:o!r;ld * de*ar !"
  
# print given string
print ("Original String : " + givenStr) 
  
# using filter() to  
# remove special chars 
givenStr = filter(lambda i: i not in sp_chars, givenStr) 
  
# printing resultant string  
print ("After Remove special char : " + str(givenStr)) 

After executing the program, the output will be:

Original String : Hel;lo *w:o!r;ld * de*ar !
After Remove special char : Hello world  dear

Recommended Python Programs

RELATED ARTICLES

Most Popular

Dominic
32271 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6641 POSTS0 COMMENTS
Nicole Veronica
11807 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11870 POSTS0 COMMENTS
Shaida Kate Naidoo
6755 POSTS0 COMMENTS
Ted Musemwa
7030 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS