Python Learning: Maketrans and Translate methods

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.