My string module doesn't have maketrans or translate functions

Chris Angelico rosuav at gmail.com
Thu Apr 11 11:45:05 EDT 2013


On Fri, Apr 12, 2013 at 1:30 AM, Lamb <lambandme at gmail.com> wrote:
> import string
> s = "string. With. Punctuation?"
> out = s.translate(string.maketrans("",""), string.punctuation)

Try this instead:

import string
s = "string. With. Punctuation?"
out = s.translate(str.maketrans("", "", string.punctuation))

Due to the changes in string handling (s is a Unicode string in Python
3, not a string of bytes), str.translate() got changed. Check out
help(str.maketrans) and help(str.translate) in interactive Python for
more details.

ChrisA



More information about the Python-list mailing list