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