python remove punctuation from string code example
Example 1: python remove last characters from string
st = "abcdefghij"
st = st[:-1]
Example 2: remove punctuation from string python
import re
s = "string. With. Punctuation?"
s = re.sub(r'[^\w\s]','',s)
s = "string. With. Punctuation?"
s.translate(str.maketrans('', '', string.punctuation))
Example 3: clean punctuation from string python
s.translate(str.maketrans('', '', string.punctuation))
Example 4: remove punctuations using python
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
my_str = "Hello!!!, he said ---and went."
no_punct = ""
for char in my_str:
if char not in punctuations:
no_punct = no_punct + char
print(no_punct)
Example 5: python3 strip punctuation from string
import string
translator=str.maketrans('','',string.punctuation)
string_name=string_name.translate(translator)