Python AttributeError: module 'string' has no attribute 'maketrans' Python AttributeError: module 'string' has no attribute 'maketrans' python-3.x python-3.x

Python AttributeError: module 'string' has no attribute 'maketrans'


maketrans is deprecated in favor of new static methods

The string.maketrans() function is deprecated and is replaced by new static methods, bytes.maketrans() and bytearray.maketrans(). This change solves the confusion around which types were supported by the string module. Now, str, bytes, and bytearray each have their own maketrans and translate methods with intermediate translation tables of the appropriate type.

You can use dir() to verify that whenever you have this kind of issue:

>>> import string>>>>>> dir(string)['Formatter', 'Template', '_ChainMap', '_TemplateMetaclass', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']>>>

As you can see, there is no maketrans in the resulted list above.


Py 3.9:

"abcdef".translate(str.maketrans('def', 'ghi'))