xxxxxxxxxx
# Python program to remove punctuation from a string
import string
text= 'Hello, W_orl$d#!'
# Using translate method
print(text.translate(str.maketrans('', '', string.punctuation)))
xxxxxxxxxx
#with re
import re
s = "string. With. Punctuation?"
s = re.sub(r'[^\w\s]','',s)
#without re
s = "string. With. Punctuation?"
s.translate(str.maketrans('', '', string.punctuation))
xxxxxxxxxx
import string
from nltk.tokenize import word_tokenize
s = set(string.punctuation) # !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
sentence = "Hey guys !, How are 'you' ?"
sentence = word_tokenize(sentence)
filtered_word = []
for i in sentence:
if i not in s:
filtered_word.append(i);
for word in filtered_word:
print(word,end = " ")
xxxxxxxxxx
import string
#make translator object
translator=str.maketrans('','',string.punctuation)
string_name=string_name.translate(translator)
xxxxxxxxxx
import string
words = ["hell'o", "Hi,", "bye bye", "good bye", ""]
words = [''.join(letter for letter in word if letter not in string.punctuation) for word in words if word]
print(words)
xxxxxxxxxx
import string
def remove_punctuation(text):
'''a function for removing punctuation'''
# replacing the punctuations with no space,
# which in effect deletes the punctuation marks
translator = str.maketrans('', '', string.punctuation)
# return the text stripped of punctuation marks
return text.translate(translator)
xxxxxxxxxx
import string
test_str = 'Gfg, is best: for ! Geeks ;'
test_str = test_str.translate
(str.maketrans('', '', string.punctuation))
print(test_str)