Thursday, December 26, 2024
Google search engine
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

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?