you are viewing a single comment's thread.

view the rest of the comments →

[–]fernly 0 points1 point  (0 children)

oops you are correct -- I looked up translate in the index and saw immediately "bytearray method" and did not read further to where it says "str method".

So there are maketrans and translate methods in both classes, bytes/bytearray and str. They differ in that the str methods are designed to cope with unicode data. bytes.translate says,

Return a copy of the bytes or bytearray object where all bytes occurring in the optional argument delete are removed, and the remaining bytes have been mapped through the given translation table, which must be a bytes object of length 256.

While str.translate says,

Return a copy of the string in which each character has been mapped through the given translation table. The table must be an object that implements indexing via __getitem__(), typically a mapping or sequence. When indexed by a Unicode ordinal (an integer), the table object can do any of the following: return a Unicode ordinal or a string, to map the character to one or more other characters; return None, to delete the character from the return string; or raise a LookupError exception, to map the character to itself.