1.string.maketrans (INSTR,OUTSTR) returns a translation table
The conversion was completed when the Maketrans function was called. For example String.maketrans (' ABCD ', ' ABCD '), the location of the original "ABCD" in the translation table after completion of the call has been replaced by "ABCD".
2.str.translate (Table,del)
Converts the generated conversion table in 1 as a parameter, converts the portion of Str containing InStr to OUSTR, and eventually returns the replacement-complete string
3. For example:
Import stringinstr= ' abcd ' outstr= ' abcd ' test_str = ' abcd123 ' table = String.maketrans (instr,outstr) ' Code a ' ' Print (test _str.translate (table) "Code two '" Print (test_str.translate (table, ' 123 ')) ' Code three ' ' Print (test_str.translate (None, ' 123 '))
#代码一: replace ABCD in tets_str with ABCD and the output is: ABCD123
#代码二: After deleting 123 in TETS_STR and then replacing ABCD with ABCD, the output is: ABCD123
#代码三: Remove 123 from TETS_STR and the output is: ABCD
Python Learning: Maketrans and Translate methods