Explained
h2>
use translate to replace specific characters in a string, such as 12345 for aeiou. Using translate requires the maketrans method to build the replacement table
note: python2’s maketrans method needs to be imported, whereas python3 is built in. In python3, using the syntax of python2 to import: ImportError: cannot import name ‘maketrans’
str.maketrans()
python document interpretation
Help on built-in function maketrans in str:
str.maketrans = maketrans(...)
Return a translation table usable for str.translate().
If there is only one argument, it must be a dictionary mapping Unicode
ordinals (integers) or characters to Unicode ordinals, strings or None.
Character keys will be then converted to ordinals.
If there are two arguments, they must be strings of equal length, and
in the resulting dictionary, each character in x will be mapped to the
character at the same position in y. If there is a third argument, it
must be a string, whose characters will be mapped to None in the result.
str.translate()
python document interpretation
Help on method_descriptor in str:
str.translate = translate(self, table, /)
Replace each character in the string using the given translation table.
table
Translation table, which must be a mapping of Unicode ordinals to
Unicode ordinals, strings, or None.
The table must implement lookup/indexing via __getitem__, for instance a
dictionary or list. If this operation raises LookupError, the character is
left untouched. Characters mapped to None are deleted.
Example
h2>
replace aeiou with 12345
trantab = str.maketrans("aeiou", "12345")
print ("EXAMPLE:aeiou".translate(trantab))
Output
is
EXAMPLE:12345
Read More:
- TypeError: translate() takes exactly one argument (2 given)
- Method of ignoring case in Python string comparison
- Several calculation methods of Python execution time
- Python string prefix
- Several implementation methods of Python timing task
- A repeated string is composed of two identical strings. For example, abcabc is a repeated string with length of 6, while abcba does not have a duplicate string. Given any string, please help Xiaoqiang find the longest repeated substring.
- Methods of modifying index and columns names by dataframe in pandas
- Python TypeError: not all arguments converted during string formatting [Solved]
- Common attributes and methods of list and map in Dar
- Several methods of deleting all empty lines in text under Linux
- Python: How to Fix “Ord() expected string of length 1, but int found”
- python ValueError: source code string cannot contain null bytes
- Python conversion hex to string, high and low data processing
- Syntax error: invalid syntax before Python string
- TSLint:object access via string literals is disallowedtslint(no-string-literal)
- Several methods of executing multiple commands in Linux shell
- [leetcode] 926. Flip string to monotone increasing problem solving report (Python)
- Three methods of referencing DLL in Visual Studio 2015
- Two methods of cmake in VTK program
- Unity short string intercepts long string