close

How to remove Special Characters From the String in Python

Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about how to remove Special Characters From the String in Python. so without wasting time lets learn about of this.

How to remove Special Characters From the String in Python

  1. remove Special Characters From the String in Python

    To remove Special Characters From the String in Python Just use str.replace() you can remove special characters from the string in python. So lets learn about of this through an example: s="Hello guys & how are you" s1=s.replace("&","") print (s1) Output : Hello guys how are you

  2. How to remove Special Characters From the String in Python

    To remove Special Characters From the String in Python Just Use re.sub() you can remove special characters from the string in python. So lets learn about of this through an example: var1="Hello guys &%$ how are you" import re var2=re.sub("[&%$]","",var1) print (var2) Output : Hello guys how are you

Method 1: Using str.replace()

By using str.replace() you can remove special char. So lets learn about of this through an example:

s="Hello guys & how are you"
s1=s.replace("&","")
print (s1)

Output :

Hello guys  how are you

Method 2: Using re.sub()

By using re.sub() you can remove special characters. So lets learn about of this through an example:

var1="Hello guys &%$ how are you"
import re
var2=re.sub("[&%$]","",var1)
print (var2)

Output :

Hello guys  how are you

Method 3: Using filter()

By using filter() you can remove special characters from the string. So lets learn about of this through an example:

var1="hello guys, How are you??&"
var2=filter(str.isalpha,var1)
s1="".join(var2)
print (s1)

Output :

helloguysHowareyou

Conclusion

It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?

Also, Read

Leave a Comment