https://github.com/python/cpython/commit/9b8d59c136c8016f88844b716ddbd11bc7defb84 commit: 9b8d59c136c8016f88844b716ddbd11bc7defb84 branch: main author: kovan <[email protected]> committer: vstinner <[email protected]> date: 2026-02-10T11:13:40+01:00 summary:
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 <[email protected]> files: M Doc/library/stdtypes.rst diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index b8c079faa93d6d..ffb5a053a6dce8 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -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. _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
