From 30e9c5bb40a7dc5811636d6741dddad2320483a3 Mon Sep 17 00:00:00 2001 From: kovan Date: Tue, 3 Feb 2026 23:15:26 +0100 Subject: [PATCH] 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 --- Doc/library/stdtypes.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 99479091cd5bd2..b4cd5b603fdc5b 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -2833,6 +2833,12 @@ 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'``:: + + >>> '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.