In this post, we will learn how to remove punctuation from a string in Python, with a detailed explanation and example.
There are two different ways to remove punctuations in Python:
- Using for-loop &
- Using Python functions
But before we jump into the programming part, let’s discuss very basic questions such as what is punctuation and why do we need to remove it from the string.
What is Punctuation?
Punctuation marks are special symbols, such as full stops, commas, question marks, exclamation points, and more, used in writing to separate sentences and clarify meaning.
Some symbols of punctuations are: !"#$%&'()*+,-./:;<=>?@[]^_`{|}~
Why do we need to remove Punctuations from the string?
Removing punctuations from the string is a very common task when preparing datasets for any text classifier in machine learning, and it is also essential when dealing with NLP (Natural Language Processing) related tasks. This process helps improve model performance.
That’s why it’s important to know how to remove punctuation from the string, and it’s also very simple. Now that we know all the basic things, let’s jump into the programming part.
Remove Punctuations From a String in Python Using for-loop
Let’s start with the algorithm first and understand each step of how to write this program. After that, we can move on to the coding part and see the output.
Algorithm
- Ask the user to enter their sentence and store it in a variable called
sentence
. - Create one variable called
punctuations
and store each punctuation in string format (punctuations = "!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"
). - Create a variable called
new_string
for storing sentences without punctuations initially it is empty. - Iterate in a
sentence
using for-loop and inside for-loop check:if i not in punctuations
then donew_string +=i
- At the end, outside of the for-loop print
new_string
.
Few prerequisites for this program: |
---|
How to take input from the user |
For-loop |
Python Strings |
Source Code
sentence = input("Please enter your sentence: ")
punctuations = "!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"
new_string = ''
for i in sentence:
if i not in punctuations:
new_string +=i
print(new_string)
Output
Please enter your sentence: Hello! how are you? am fine...
Hello how are you am fine
Now let’s modify this program and write it using Python functions and list comprehension.
Remove Punctuations From a String in Python Using Function
Few prerequisites for this program: |
---|
How to take input from the user |
Python Strings & String methods (join() ) |
Python Functions |
List comprehension |
Python built-in string module. |
Source Code
import string
# define a function
def remove_punctuation(text):
return ''.join(char for char in text if char not in string.punctuation)
# user input
sentence = input("Please enter your sentence: ")
# call a function
print(remove_punctuation(sentence))
Output
Please enter your sentence: Hello! how are you? let's start learning Python.
Hello how are you lets start learning Python
This is all about how to remove punctuation from a string in Python. I hope this post adds some value to your life thank you for reading.