On 29 Apr 2005 21:27:18 -0700, "M.E.Farmer" <[EMAIL PROTECTED]> wrote:
>Bengt Richter wrote: >> Just thought None as the first argument would be both handy and >mnemonic, >> signifying no translation, but allowing easy expression of deleting >characters, >> e.g., >> >> s = s.translate(None, 'badcharshere') >> >> Regards, >> Bengt Richter > >What's wrong with : > >s = s.replace('badchars', "") That's not what translate does with the badchars, which is a set of characters, each and all of whose instances in the source string will be deleted from the source string. Something like for c in badchars: s = s.replace(c,'') > >It seems obvious to replace a char ( to me anyway ) with a blank >string, rather than to 'translate' it to None. >I am sure you have a reason what am I missing ? >M.E.Farmer The first argument is normally a 256-character string that serves as a table for converting source characters to destination, so s.translate(table, bad) does something like s = ''.join([table[ord(c)] for c in s if c not in bad] >>> help(str.translate) Help on method_descriptor: translate(...) S.translate(table [,deletechars]) -> string Return a copy of the string S, where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256. So just to delete, you wind up constructinf a table argument that is just 1:1 as in >>> 'abcde'.translate(''.join([chr(i) for i in xrange(256)]), 'tbd') 'ace' and the answer to the question, "What translation does such a table do?" is "None" ;-) Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list