liveBook · Manning

Translate

As explained in 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.

Python’s translate method returns a new string after transforming or removing characters according to a translation table. Use str.maketrans to create mappings, then pass the table to translate. section 6.4.5

The core rules are compact:

  • maketrans can pair characters positionally: the nth character in the first string maps to the nth character in the second. section 6.4.5
  • A translation table can also remove selected characters by supplying them as the third argument. section 6.4.5
  • Because strings are immutable, translate produces a modified copy rather than changing the original string. section 6.4.5
  • Create a reusable table outside a processing loop; rebuilding it for every line adds unnecessary work. section 6.10.2

Translate operators or remove punctuation

table = "~x ^ (y % z)".maketrans("~^()", "!&[]") "~x ^ (y % z)".translate(table) # '!x & [y % z]' remove = str.maketrans("", "", "()~^%") "~x ^ (y % z)".translate(remove) # 'x y z' section 6.4.5

Normalize text before word processing

Lowercase a line, remove punctuation in one operation, split it into words, and write one word per line. This is more compact than repeatedly calling replace for each punctuation character. section 6.10.2 section 21.2.2

import string                                            #1
punct = str.maketrans("",  "", string.punctuation)       #2
 
with open("moby_01.txt") as infile: 
    with open("moby_01_clean.txt", "w") as outfile:
        for line in infile:
            # make all one case
            cleaned_line = line.lower()
        
            # remove punctuation
            cleaned_line = cleaned_line.translate(punct) #3
        
            # split into words
            words = cleaned_line.split()
            cleaned_words = "\n".join(words)             #4
            # write all words for line
            outfile.write(cleaned_words)

Follow the complete file-cleaning pipeline using a punctuation-removal table reused for every input line. section 6.10.2

How The Quick Python Book, Fourth Edition covers it

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 it assumes
Assumes general Python and string-processing knowledge while reinforcing immutability, translation tables, and normalization choices.
What you can do after
Apply it via Python string methods to transform or remove many characters efficiently in text-processing pipelines.
How it connects
Connects translate with string immutability, str.maketrans, replace, split, lowercase conversion, and text normalization.

Also explained in

Compare how 2 books explain translate →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.