Skip to content

Commit 5828e9b

Browse files
kovanclaude
andcommitted
gh-72798: Add mapping example to str.translate documentation
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>
1 parent cfeede8 commit 5828e9b

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

Doc/library/stdtypes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,6 +2846,12 @@ expression support in the :mod:`re` module).
28462846
You can use :meth:`str.maketrans` to create a translation map from
28472847
character-to-character mappings in different formats.
28482848

2849+
The following example uses a mapping to replace ``'a'`` with ``'X'``,
2850+
``'b'`` with ``'Y'``, and delete ``'c'``::
2851+
2852+
>>> 'abc123'.translate({ord('a'): 'X', ord('b'): 'Y', ord('c'): None})
2853+
'XY123'
2854+
28492855
See also the :mod:`codecs` module for a more flexible approach to custom
28502856
character mappings.
28512857

0 commit comments

Comments
 (0)