mirror of
https://github.com/python/cpython.git
synced 2026-02-21 22:50:55 +00:00
gh-72798: Add mapping example to str.translate documentation (#144454)
Add an example showing how to use str.translate() with a dictionary mapping directly, demonstrating character replacement and deletion. Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
2c1ca6bb5b
commit
9b8d59c136
1 changed files with 8 additions and 0 deletions
|
|
@ -2870,6 +2870,14 @@ expression support in the :mod:`re` module).
|
|||
You can use :meth:`str.maketrans` to create a translation map from
|
||||
character-to-character mappings in different formats.
|
||||
|
||||
The following example uses a mapping to replace ``'a'`` with ``'X'``,
|
||||
``'b'`` with ``'Y'``, and delete ``'c'``:
|
||||
|
||||
.. doctest::
|
||||
|
||||
>>> 'abc123'.translate({ord('a'): 'X', ord('b'): 'Y', ord('c'): None})
|
||||
'XY123'
|
||||
|
||||
See also the :mod:`codecs` module for a more flexible approach to custom
|
||||
character mappings.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue