Easy Python text featured-maketrans and translate

Source: Internet
Author: User

Scene:

To filter certain characters of a string, we start with an example


1. Create a Character map, which is what the Maketrans method does, which returns a map of a string, meaning: If A is present in the string, it becomes the corresponding 1, and so on, b->2,c->3

2. Use the Translate method to get rid of the characters inside the string

Attention:

1. The length of the mapping two times must be consistent, or error, that is, ' abc ' must be for ' 123 ' or ' 234 ' and so on, can not be ' abc ' corresponds to 'a ', this time error

>>> Tb=str.maketrans (' abc ', ' Traceback ') (most recent):  File "<pyshell#26>", line 1, in &L T;module>    
2. It is also important to note that the translate method of the string only accepts one parameter, while the bytes translate method is capable of receiving two parameters, and the second parameter can quickly implement the Delete method


Here are some of the translate application methods: (Note: In order to quickly implement the deletion, so the following methods are used bytes translate)

Modify some strings:


Delete some strings:


Keep certain characters:



Right here, thank you.

------------------------------------------------------------------

Click to jump 0 basic python-Catalogue






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Easy Python text featured-maketrans and translate

Related Article

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.