#with re
import re
s = "string. With. Punctuation?"
s = re.sub(r'[^ws]','',s)
#without re
s = "string. With. Punctuation?"
s.translate(str.maketrans('', '', string.punctuation))
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 = " ")
import string
sentence = "Hey guys !, How are 'you' ?"
no_punc_txt = ""
for char in sentence:
if char not in string.punctuation:
no_punc_txt = no_punc_txt + char
print(no_punc_txt); # Hey guys How are you
# or:
no_punc_txt = sentence.translate(sentence.maketrans('', '', string.punctuation))
print(no_punc_txt); # Hey guys How are you
# 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)))
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)
# define punctuation
punctuations = '''!()-[]{};:'",<>./?@#$%^&*_~'''
my_str = "Hello!!!, he said ---and went."
# To take input from the user
# my_str = input("Enter a string: ")
# remove punctuation from the string
no_punct = ""
for char in my_str:
if char not in punctuations:
no_punct = no_punct + char
# display the unpunctuated string
print(no_punct)