liveBook · Manning

String module

As explained in The Quick Python Book, Fourth Edition

string is a Python standard-library module providing constants and utilities for common string-processing tasks.

Python’s string module supplies reusable character constants for string-processing tasks, especially when code needs a defined set of punctuation, digits, whitespace, or ASCII letters. section 6.4.7

Useful constants include:

  • string.punctuation: punctuation characters used to build a translation table. section 6.10.2
  • string.whitespace: characters Python treats as whitespace on the current system. section 6.4.7
  • string.digits, string.hexdigits, and string.octdigits: decimal, hexadecimal, and octal digit sets. section 6.4.7
  • string.ascii_lowercase, string.ascii_uppercase, and string.ascii_letters: lowercase, uppercase, and combined ASCII letters. section 6.4.7

Remove punctuation efficiently

Create the translation table once, then reuse it while processing lines: import string; punct = str.maketrans("", "", string.punctuation); cleaned = line.lower().translate(punct). Building the table outside the loop avoids repeating the same operation for every line. section 6.10.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)

Shows a complete file-cleaning pipeline that lowercases text, removes punctuation, splits lines into words, and writes one word per line. section 6.10.2

Import string wherever string.punctuation or another module constant is used. A generated module that omits this import raises NameError; after fixing an already loaded module, reload it or restart the session. section 10.8.2

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains named constants and demonstrates reusable translation tables, but stops short of implementation internals.
Examples
Uses punctuation removal, character-set membership, digit and case tests, and Moby-Dick word-cleaning files.
What it assumes
Assumes familiarity with strings, methods, imports, loops, and file processing.
What you can do after
Apply the module in text-cleaning programs with str.maketrans(), translate(), and reusable character constants.
How it connects
Connects string constants with string methods, translation tables, file processing, and Python modules.

Also explained in

Compare how 2 books explain string module →

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.