Skip to content

Commit 79b98df

Browse files
committed
Issue #21279: Flesh out str.translate docs
Initial patch by Kinga Farkas, Martin Panter, and John Posner.
1 parent 5c28e9f commit 79b98df

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

Doc/library/stdtypes.rst

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,21 +1976,22 @@ expression support in the :mod:`re` module).
19761976
"They're Bill's Friends."
19771977

19781978

1979-
.. method:: str.translate(map)
1979+
.. method:: str.translate(table)
19801980

1981-
Return a copy of the *s* where all characters have been mapped through the
1982-
*map* which must be a dictionary of Unicode ordinals (integers) to Unicode
1983-
ordinals, strings or ``None``. Unmapped characters are left untouched.
1984-
Characters mapped to ``None`` are deleted.
1981+
Return a copy of the string in which each character has been mapped through
1982+
the given translation table. The table must be an object that implements
1983+
indexing via :meth:`__getitem__`, typically a :term:`mapping` or
1984+
:term:`sequence`. When indexed by a Unicode ordinal (an integer), the
1985+
table object can do any of the following: return a Unicode ordinal or a
1986+
string, to map the character to one or more other characters; return
1987+
``None``, to delete the character from the return string; or raise a
1988+
:exc:`LookupError` exception, to map the character to itself.
19851989

19861990
You can use :meth:`str.maketrans` to create a translation map from
19871991
character-to-character mappings in different formats.
19881992

1989-
.. note::
1990-
1991-
An even more flexible approach is to create a custom character mapping
1992-
codec using the :mod:`codecs` module (see :mod:`encodings.cp1251` for an
1993-
example).
1993+
See also the :mod:`codecs` module for a more flexible approach to custom
1994+
character mappings.
19941995

19951996

19961997
.. method:: str.upper()

Objects/unicodeobject.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13077,11 +13077,12 @@ unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z)
1307713077
PyDoc_STRVAR(translate__doc__,
1307813078
"S.translate(table) -> str\n\
1307913079
\n\
13080-
Return a copy of the string S, where all characters have been mapped\n\
13081-
through the given translation table, which must be a mapping of\n\
13082-
Unicode ordinals to Unicode ordinals, strings, or None.\n\
13083-
Unmapped characters are left untouched. Characters mapped to None\n\
13084-
are deleted.");
13080+
Return a copy of the string S in which each character has been mapped\n\
13081+
through the given translation table. The table must implement\n\
13082+
lookup/indexing via __getitem__, for instance a dictionary or list,\n\
13083+
mapping Unicode ordinals to Unicode ordinals, strings, or None. If\n\
13084+
this operation raises LookupError, the character is left untouched.\n\
13085+
Characters mapped to None are deleted.");
1308513086

1308613087
static PyObject*
1308713088
unicode_translate(PyObject *self, PyObject *table)

0 commit comments

Comments
 (0)