Many times while working with Python strings, we have a problem in which we need to remove certain characters from strings. This can have application in data preprocessing in Data Science domain and also in day-day programming. Lets discuss certain ways in which we can perform this task.
Method #1 : Using loop + punctuation string
This is brute way in which this task can be performed. In this, we check for the punctuations using raw string which contain punctuations and then we construct string removing those punctuations.
# Python3 code to demonstrate working of # Removing punctuations in string # Using loop + punctuation string # initializing string test_str = "Gfg, is best : for ! Geeks ;" # printing original string print("The original string is : " + test_str) # initializing punctuations string punc = '''!()-[]{};:'"\, <>./?@#$%^&*_~''' # Removing punctuations in string # Using loop + punctuation string for ele in test_str: if ele in punc: test_str = test_str.replace(ele, "") # printing result print("The string after punctuation filter : " + test_str) |
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Method #2 : Using regex
The part of replacing with punctuation can also be performed using regex. In this, we replace all punctuation by empty string using certain regex.
# Python3 code to demonstrate working of # Removing punctuations in string # Using regex import re # initializing string test_str = "Gfg, is best : for ! Geeks ;" # printing original string print("The original string is : " + test_str) # Removing punctuations in string # Using regex res = re.sub(r'[^\w\s]', '', test_str) # printing result print("The string after punctuation filter : " + res) |
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks
Recommended Posts:
- Python - Remove Punctuation Tuples
- string.punctuation in Python
- Python program to Sort Strings by Punctuation count
- Extract punctuation from the specified column of Dataframe using Regex
- Python - Remove front K characters from each string in String List
- Python - Remove String from String List
- Remove all duplicates from a given string in Python
- Ways to remove i'th character from string in Python
- Python | Remove spaces from a string
- Python | Ways to remove numeric digits from given string
- Python | Remove the given substring from end of string
- Python | Ways to remove n characters from start of given string
- Python | Remove unwanted spaces from string
- Python - Remove suffix from string list
- Python - Remove Initial character in String List
- Python - Remove Rear K characters from String List
- Python - Remove all consonants from string
- Python - Ways to remove multiple empty spaces from string List
- Python | Remove substring list from String
- Python | Remove List elements containing given String character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

