Translate
str.translate is a Python string method that returns a copy with characters transformed or deleted according to a translation table.
The Quick Python Book, Fourth Edition
translate is a Python string method that returns a copy of a string with characters transformed or removed according to a translation table.
- How it is used
- Normalizes text and performs coordinated character substitutions or removals before splitting, counting, or writing words.
- How deep it goes
- Explains translation tables, positional mappings, removal, immutability, and loop efficiency through compact runnable examples.
- Examples
- Uses operator conversion, punctuation removal, word normalization, and cleaning the Moby-Dick text file.
- What you can do after
- Apply it via Python string methods to transform or remove many characters efficiently in text-processing pipelines.
Read how The Quick Python Book, Fourth Edition explains translate →
Tiny Python Projects: Learn coding and testing with puzzles and games
str.translate() is a Python string method that returns a new string by mapping characters according to a translation table.
- How it is used
- Maps characters in strings for compact, one-pass substitutions while preserving characters absent from the translation table.
- How deep it goes
- Defines translation tables, Unicode ordinal mappings, replacement strings, deletion with None, and lookup behavior.
- Examples
- Demonstrates encoding phone-like digit strings while leaving letters, spaces, hyphens, and equals signs unchanged.
- What you can do after
- Apply it via str.maketrans() and str.translate() to build reusable character-substitution transformations.
Read how Tiny Python Projects: Learn coding and testing with puzzles and games explains translate →
Related concepts
- replace methodalternative to · A replace operation substitutes selected matching content or values with specified alternatives in a string, collection, or other object, either returning a result or modifying the object.
- translation tableuses · A translation table is a mapping from source items to corresponding replacement, standardized, or translated values.
- stringalternative to · A string is a programming-language value representing an ordered sequence of characters or character units as text.
- loopcontrasts with · A loop is a programming control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

