'How to remove punctuation marks from a string in Python 3.x using .translate()?

I want to remove all punctuation marks from a text file using .translate() method. It seems to work well under Python 2.x but under Python 3.4 it doesn't seem to do anything.

My code is as follows and the output is the same as input text.

import string
fhand = open("Hemingway.txt")
for fline in fhand:
    fline = fline.rstrip()
    print(fline.translate(string.punctuation))


Solution 1:[1]

You have to create a translation table using maketrans that you pass to the str.translate method.

In Python 3.1 and newer, maketrans is now a static-method on the str type, so you can use it to create a translation of each punctuation you want to None.

import string

# Thanks to Martijn Pieters for this improved version

# This uses the 3-argument version of str.maketrans
# with arguments (x, y, z) where 'x' and 'y'
# must be equal-length strings and characters in 'x'
# are replaced by characters in 'y'. 'z'
# is a string (string.punctuation here)
# where each character in the string is mapped
# to None
translator = str.maketrans('', '', string.punctuation)

# This is an alternative that creates a dictionary mapping
# of every character from string.punctuation to None (this will
# also work)
#translator = str.maketrans(dict.fromkeys(string.punctuation))

s = 'string with "punctuation" inside of it! Does this work? I hope so.'

# pass the translator to the string's translate method.
print(s.translate(translator))

This should output:

string with punctuation inside of it Does this work I hope so

Solution 2:[2]

The call signature of str.translate has changed and apparently the parameter deletechars has been removed. You could use

import re
fline = re.sub('['+string.punctuation+']', '', fline)

instead, or create a table as shown in the other answer.

Solution 3:[3]

In python3.x ,it can be done using :

import string
#make translator object
translator=str.maketrans('','',string.punctuation)
string_name=string_name.translate(translator)

Solution 4:[4]

I just compared the three methods by speed. translate is slower than re.sub (with precomilation) in about 10 times. And str.replace is faster than re.sub in about 3 times. By str.replace I mean:

for ch in string.punctuation:                                                                                                     
    s = s.replace(ch, "'") 

Solution 5:[5]

Late answer, but to remove all punctuation on python >= 3.6, you can also use:

import re, string

clean_string = re.sub(rf"[{string.punctuation}]", "", dirty_string)

Demo

Solution 6:[6]

In Python 3.6 you can use the following to remove punctuation:

import string

your_string.translate(str.maketrans('', '',string.punctuation))

The .maketrans() method takes three arguments - the first two are empty strings, and the third is the list of punctuation we want to remove. This tells the function to replace all punctuation with 'None'.

Additionally, you can view the punctuation attribute that comes with the string library by running:

print(string.punctuation)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Mayank Kumar
Solution 4 imbolc
Solution 5 Pedro Lobito
Solution 6 Gospel77