diff -r 40f1b06ec989 Doc/library/stdtypes.rst --- a/Doc/library/stdtypes.rst Sun Dec 18 01:37:12 2016 +0000 +++ b/Doc/library/stdtypes.rst Thu Dec 29 02:34:23 2016 -0800 @@ -2022,6 +2022,18 @@ You can use :meth:`str.maketrans` to create a translation map from character-to-character mappings in different formats. + Another approach is to use **defaultdict** as translation table. Below example + keeps only lower case ASCII alphabets in a string:: + + >>> from collections import defaultdict + >>> dd = defaultdict(lambda:None, {ord(ch):ch for ch in 'qwertyuiopasdfghjklzxcvbnm'}) + >>> '1. Keep only lower case alphabets'.translate(dd) + 'eeponlylowercasealphabets' + + See :class:`collections.defaultdict` for more ways to create defaultdict. + Note that key in translate table should be an ordinal that is why + ``ord(ch)`` is used as key in defaultdict example. + See also the :mod:`codecs` module for a more flexible approach to custom character mappings.