19
votes

I'm a Python newbie.

How come this doesn't work in Python 3.1?

from string import maketrans   # Required to call maketrans function.

intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)

str = "this is string example....wow!!!";
print str.translate(trantab);

When I executed the above code, I get the following instead:

Traceback (most recent call last):
  File "<pyshell#119>", line 1, in <module>
    transtab = maketrans(intab, outtab)
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/string.py", line 60, in maketrans
    raise TypeError("maketrans arguments must be bytes objects")
TypeError: maketrans arguments must be bytes objects

What does "must be bytes objects" mean? Could anyone please help post a working code for Python 3.1 if it's possible?

9
It doesn't work, because string.maketrans doesn't do what you think. Which is why it was deprecated in 3.1 for bytes.maketrans. You should use str.maketrans instead of string.maketrans for strings, and bytes.maketrans for bytes. - Lennart Regebro
To see the difference yourself, compare the output of print(str.maketrans("aeiou","12345")) --which is {97: 49, 101: 50, 105: 51, 111: 52, 117: 53} --to the output of print(bytes.maketrans(b"aeiou",b"12345")) (which is tool long and ugly to show here.) - MarkHu

9 Answers

11
votes

Strings are not bytes.

This is a simple definition in Python 3.

Strings are Unicode (which are not bytes) Unicode strings use "..." or '...'

Bytes are bytes (which are not strings) Byte strings use b"..." or b'...'.

Use b"aeiou" to create a byte sequence composed of the ASCII codes for certain letters.

33
votes

You don't need to use bytes.maketrans() when str would be simpler and eliminate the need for the 'b' prefix:

print("Swap vowels for numbers.".translate(str.maketrans('aeiou', '12345')))
14
votes

Stop trying to learn Python 3 by reading Python 2 documentation.

intab = 'aeiou'
outtab = '12345'

s = 'this is string example....wow!!!'

print(s.translate({ord(x): y for (x, y) in zip(intab, outtab)}))
8
votes

In Python 3, 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.

5
votes
"this is string example....wow!!!".translate(str.maketrans("aeiou","12345"))

This works, and no additional byte transformation. I don't know the reason why to use byte instead of str.

1
votes

If you absolutely insist on working with 8-bit bytes:

>>> intab = b"aeiou"
>>> outtab = b"12345"
>>> trantab = bytes.maketrans(intab, outtab)
>>> strg = b"this is string example....wow!!!";
>>> print(strg.translate(trantab));
b'th3s 3s str3ng 2x1mpl2....w4w!!!'
>>>
0
votes

Hey here is the simple one liner which worked perfectly for me

import string
a = "Learning Tranlate() Methods"
print (a.translate(bytes.maketrans(b"aeiou", b"12345")))*
OUTPUT
0
votes

Here is an example of performing translation and deletion, in Python 2 or 3:

import sys


DELETE_CHARS = '!@#$%^*()+=?\'\"{}[]<>!`:;|\\/-,.'


if sys.version_info < (3,):

    import string

    def fix_string(s, old=None, new=None):
        if old:
            table = string.maketrans(old, new)
        else:
            table = None
        return s.translate(table, DELETE_CHARS)

else:

    def fix_string(s, old='', new=''):
        table = s.maketrans(old, new, DELETE_CHARS)
        return s.translate(table)
0
votes

maketrans is a string function

use below logic to use translate using maketrans

print('maketrans' , '& translate')
intab = "aeiou"
outtab = "12345"
str = "Fruits are delicious and healthy!!!"
trantab = str.maketrans(intab, outtab)
print (str.translate(trantab))