str.translate gives TypeError – Translate takes one argument (2 given), worked in Python 2

If all you are looking to accomplish is to do the same thing you were doing in Python 2 in Python 3, here is what I was doing in Python 2.0 to throw away punctuation and numbers:

text = text.translate(None, string.punctuation)
text = text.translate(None, '1234567890')

Here is my Python 3.0 equivalent:

text = text.translate(str.maketrans('','',string.punctuation))
text = text.translate(str.maketrans('','','1234567890'))

Basically it says ‘translate nothing to nothing’ (first two parameters) and translate any punctuation or numbers to None (i.e. remove them).

Leave a Comment