Wednesday, February 20, 2019

Removing Punctuation's from a string

Problem
You have a text and you want to remove punctuations from it. Example:
in:
"Hello! It is time to remove punctuations here. It is easy, you will see."

out:
"Hello It is time to remove punctuations here It is easy you will see"
Solution
Let’s see a Python 3 solution:
1
2
3
4
5
>>> import string
>>> tr = str.maketrans("", "", string.punctuation)
>>> s = "Hello! It is time to remove punctuations here. It is easy, you will see."
>>> s.translate(tr)
'Hello Its time to remove punctuations here Its easy youll see'

No comments:

Post a Comment