Python | Remove Punctuations From String

Punctuations are crazy grammatical symbols which is used in English Language for making written text more readable. But what if for some reason, in your program you need to remove these punctuation symbols from strings or a text files. In this article, I’ll discuss about How to use Python Code for removing punctuations from string?.

Python Code for Removing Punctuations

Regex for removing punctuations from string is => ”’!()-[]{};:'”\,<>./?@#$%^&*_~”’

# Python code for removing punctuations from string

# Defining a Regex for matching punctuations in strings
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

# Define a String from which punctuations to be removed
a_string = "This website----- is called Computer!! Science!!! Hub!!!!!"

string_without_punctuation = ""

for char in a_string:
	if char not in punctuations:
		string_without_punctuation = string_without_punctuation + char

# Printing out string having no punctuations
print("String before removing punctuations =>", a_string)
print("String after removing punctuations =>", string_without_punctuation)

Output of Above Code

String before removing punctuations => This website----- is called Computer!! Science!!! Hub!!!!!
String after removing punctuations => This website is called Computer Science Hub

Leave a Comment