Remove punctuation from string in Python

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

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"




Fill in your details below or click an icon to log in: 徽标

You are commenting using your account. Log Out /  更改 )

Google photo

You are commenting using your Google account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s