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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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