Python 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)) 

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *