Python Program to Remove Punctuations From a String

Strings:

A string in Python is an ordered collection of characters that is used to represent and store text-based data. Strings are stored in a contiguous memory area as individual characters. It can be accessed in both forward and backward directions. Characters are merely symbols. Strings are immutable Data Types in Python, which means they can’t be modified once they’ve been generated.

Punctuation:

Punctuation is the practice, action, or method of putting points or other small marks into texts to help comprehension; the division of text into phrases, clauses, and so on.

Punctuation is really effective. They have the ability to completely alter the meaning of a sentence.

Given a string, the task is to remove the punctuations from the given string in python.

Examples:

Example1:

Input:

given_string="BTechGeeks, is best : for ! Python.?[]() ;"

Output:

printing the given string after removing the punctuations : 
BTechGeeks is best for Python

Example2:

Input:

given_string="pythond dsf,;]]][-*&$%@#^()!or ! Python.?[]() ;"

Output:

printing the given string after removing the punctuations : 
pythond dsfor  Python

Remove Punctuations From a String in Python

When working with Python strings, we frequently run into situations where we need to remove specific characters. This can be used for data preprocessing in the Data Science domain as well as day-to-day programming. Let’s look at a few different approaches to doing this work.

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1:Using for loop and string concatenation

This is the most brute method of accomplishing this operation. We do this by checking for punctuations in raw strings that contain punctuations, and then constructing a string without those punctuations.

To begin, we’ll create a string of punctuations. Then, using a for loop, we iterate over the specified string.

The membership test is used in each iteration to determine if the character is a punctuation mark or not. If the character is not punctuation, we add (concatenate) it to an empty string. Finally, we show the string that has been cleaned up.

Below is the implementation:

# taking a string which stores all the punctuations and
# initialize it with some punctuations
punctuationString = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
# given string which we want  to remove the punctuation marks
given_string = "BTechGeeks, is best: for !Python.?[]() ;"

# Taking a empty which stores all the characteers of original string without punctuations
noPunctuationString = ""
# removing all punctuations from the string
# Traversing the original string
for character in given_string:
  # if character not in punctuationString which means it is not a punctuation
  # hence concate this character to noPunctuationString
    if character not in punctuationString:
        noPunctuationString = noPunctuationString + character

# printing the given string after removing the punctuations
print("printing the given string after removing the punctuations : ")
print(noPunctuationString)

Output:

printing the given string after removing the punctuations : 
BTechGeeks is best for Python

Method #2:Using for loop and replace() function

This is the most brute method of accomplishing this operation. We do this by checking for punctuations in raw strings that contain punctuations, and then constructing a string without those punctuations.

Approach:

  • Make a string out of all the punctuation characters.
  • Create a for loop and an if statement for each iteration such that if a punctuation character is detected, it is replaced by a white space.

Below is the implementation:

# taking a string which stores all the punctuations and
# initialize it with some punctuations
punctuationString = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
# given string which we want  to remove the punctuation marks
given_string = "BTechGeeks, is best: for !Python.?[]() ;"

# removing all punctuations from the string
# Traversing the original string
for character in given_string:
  # if character  in punctuationString which means it is  a punctuation
  # hence replace it with white space
    if character in punctuationString:
        given_string = given_string.replace(character, "")
# printing the given string after removing the punctuations
print("printing the given string after removing the punctuations : ")
print(given_string)

Output:

printing the given string after removing the punctuations : 
BTechGeeks is best for Python

Method #3:Using Regex

The regex module in Python allows us to work with and alter various regular expressions.

To deal with regular expressions, we’ll need to import the library listed below:

import re

To remove the punctuation marks, we’ll use re.sub(pattern, replacement, given string).
Pattern : We wish to replace the punctuation marks or the pattern of expressions with this pattern.
Replacement: The string that will be used to replace the pattern.
We’ll also utilise the re.sub() method to replace the punctuation marks with the substitute ‘ ‘, which is a white space.

Below is the implementation:

# importing regex
import re
# given string which we want  to remove the punctuation marks
given_string = "BTechGeeks, is best: for !Python.?[]() ;"
# using regex
noPunctuationString = re.sub(r'[^\w\s]', '', given_string)

# printing the given string after removing the punctuations
print("printing the given string after removing the punctuations : ")
print(noPunctuationString)

Output:

printing the given string after removing the punctuations : 
BTechGeeks is best for Python

Related Programs: