Python remove punctuation from string
In this post, you will learn how to remove punctuation from strings using the Python programming language. Many times, during working on data processing or getting valuable information or to make it more useful, we want to strip or remove all the special characters from strings. In English grammar, there are many symbols, including comma, hyphen, question mark, dash, exclamation mark, colon, semicolon, parentheses, brackets, and so on, which are called punctuation marks. There are different ways to remove punctuation from a string in Python.
Python remove punctuation using not in operator
Python provides 'not in' membership operator that evaluates to true if it does not find a variable in the specified sequence, otherwise it returns false. To remove all special characters, we use this operator and loop over the sting using for loop and take a variable that contains a list of punctuation. If the characters of the string are not in the punctuation list, then join them.
# define punctuation list to remove
punct_list = '''<>./!()-[]{}*_~;:'"\,?@#$%^&'''
string = "Hello!!!, Andy. What's going on??? Everything is fine here.:)"
# remove punctuation from the string
no_punct = ""
for x in string:
if x not in punct_list:
no_punct = no_punct + x
# display string without punctuation
print(no_punct)
Output of the above code-
Hello Andy Whats going on Everything is fine here
Python remove punctuation using in operator
We can also perform the above task with the help of only in operator. The 'in' operator is used to check if a value exists in a sequence or not. It evaluates to true if find a variable in the specified sequence and false otherwise. Here, we loop over the string and replace the punctuation with an empty string.
# define punctuation list to remove
punct_list = '''<>./!()-[]{}*_~;:'"\,?@#$%^&'''
string = "Hello!!!, Andy. What's going on??? Everything is fine here.:)"
# remove punctuation with blank string
for char in string:
if char in punct_list:
string = string.replace(char, "")
# display string without punctuation
print(string)
Output of the above code:
Hello Andy Whats going on Everything is fine here
Python remove punctuation with regex
Regular expressions are a powerful language for matching text patterns. Python provides a re module that supports the use of regex in Python. We can use regex to replace all punctuation with an empty string.
import re
# initializing string
str = "Hello!!!, Andy. What's going on??? Everything is fine here.:"
# Removing punctuations in string
# Using regex
res = re.sub(r'[^\w\s]', '', str)
# printing result
print(res)
Output of the above code:
Hello Andy Whats going on Everything is fine here
Related Articles
Find the stop words in nltk PythonPython program to multiply two numbers
Replace multiple characters Python
Python split multiple delimiters
Python split strings by comma
Remove spaces from string Python
Create word cloud using Python
How to read data from excel file using Python Pandas
How to read data from excel file in Python
Python read JSON from URL requests
Send email to multiple recipients Python
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml