Say we have a string “Hello, are you still there?”, we want to transform it into “Hello are you still there”, the question is how to do it?
Before we go ahead, we need to first define what punctuation is, it’s easy to do this by using
string.punctuation. Let’s check its value using following command:
import string print(string.punctuation)
Above code just outputs a string containing all punctuation, its content as follows:
Now let’s make the transformation using
str.translate, see following code for details:
import string # Create a string to operate on s = "Hello, are you still there?" # Create a translation table translator = str.maketrans('', '', string.punctuation) # Make the translate s = s.translate(translator) # Check the result print(s) # prints "Hello are you still there"