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

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts