Skip to content

Commit 30e9c5b

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 7e2c9bd commit 30e9c5b

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
@@ -2833,6 +2833,12 @@ expression support in the :mod:`re` module).
28332833
You can use :meth:`str.maketrans` to create a translation map from
28342834
character-to-character mappings in different formats.
28352835

2836+
The following example uses a mapping to replace ``'a'`` with ``'X'``,
2837+
``'b'`` with ``'Y'``, and delete ``'c'``::
2838+
2839+
>>> 'abc123'.translate({ord('a'): 'X', ord('b'): 'Y', ord('c'): None})
2840+
'XY123'
2841+
28362842
See also the :mod:`codecs` module for a more flexible approach to custom
28372843
character mappings.
28382844

0 commit comments

Comments
 (0)