Best way to strip punctuation from a string

One way to strip punctuation from a string is to use the str.translate() method in combination with the string.punctuation constant.

import string
s = "Hello, World!"
s = s.translate(str.maketrans('', '', string.punctuation))
print(s)

output:

Hello World

Watch a course Python - The Practical Guide

Another way is to use re.sub() method of regular expression module, which you can use to replace all punctuation characters with a space.

import re
s = "Hello, World!"
s = re.sub(r'[^\w\s]','',s)
print(s)

output:

Hello World

You can also use list comprehension to iterate over the string and only keep the characters that are not in the string of punctuation marks:

import string
s = "Hello, World!"
s = ''.join([c for c in s if c not in string.punctuation])
print(s)

output:

Hello World

You can choose the way that suits you best.