Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Standard Library: Math, Data and Text

The string module

Reference for Python's string module: ascii_letters, digits and punctuation constants, string.Template substitution, capwords and the Formatter class.

The string module is a small toolbox of text constants and helpers. Despite the name it is not where the str type lives; str is built in. This module provides ready-made character sets (letters, digits, punctuation), the Template class for simple $name substitution, the capwords() function and the Formatter class behind str.format().

Its constants are handy for validation, password generators and cleaning text, for example removing punctuation with str.translate().

Python
import string

print(string.digits)          # 0123456789
print(string.ascii_lowercase) # abcdefghijklmnopqrstuvwxyz

Try it

Python Constants and capwords

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Template substitution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Contents

Name Description
string.ascii_lowercase, ascii_uppercase, ascii_letters The 26 lowercase, 26 uppercase or all 52 ASCII letters. They do not depend on the locale.
string.digits "0123456789".
string.hexdigits, octdigits "0123456789abcdefABCDEF" and "01234567".
string.punctuation The 32 ASCII punctuation characters.
string.whitespace Space, tab, newline, carriage return, vertical tab and form feed.
string.printable Digits, letters, punctuation and whitespace (100 characters).
string.capwords(s, sep=None) Splits, capitalizes each word, and joins with single spaces.
string.Template(text) Simple substitution with $name or ${name}; $$ is a literal dollar sign.
string.Formatter Implements str.format(); subclass it to customize formatting.

Template methods

Method Behavior
substitute(mapping=None, /, **kwargs) Replaces placeholders; raises KeyError for a missing name and ValueError for a malformed placeholder.
safe_substitute(...) Leaves unknown placeholders unchanged instead of raising.
get_identifiers() List of valid placeholder names, in order of first appearance (Python 3.11+).
is_valid() True if the template has no invalid placeholders (Python 3.11+).

Usage notes

  • The constants are plain str objects. To pick random characters from them, use random.choice(); for passwords use secrets rather than random.
  • ASCII only. ascii_letters does not contain รฉ or รฑ. For Unicode-aware checks use str.isalpha() or the unicodedata module.
  • capwords() vs str.title(). title() capitalizes after every non-letter, so "don't".title() is "Don'T", while capwords() splits only on whitespace.
  • When to use Template. It is deliberately simpler than f-strings and str.format(): no attribute access, no expressions, no format specs. That makes it safer when the template text comes from users or translators.
  • Do not build SQL or shell commands with Template. Use parameterized queries and argument lists.
  • Customizing Template. Subclass it and change delimiter (default $) or idpattern to use another syntax.
  • string.letters, string.maketrans and other Python 2 names no longer exist; use str.maketrans().