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().
import string print(string.digits) # 0123456789 print(string.ascii_lowercase) # abcdefghijklmnopqrstuvwxyz
Try it
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.
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
strobjects. To pick random characters from them, userandom.choice(); for passwords usesecretsrather thanrandom. - ASCII only.
ascii_lettersdoes not containรฉorรฑ. For Unicode-aware checks usestr.isalpha()or theunicodedatamodule. capwords()vsstr.title().title()capitalizes after every non-letter, so"don't".title()is"Don'T", whilecapwords()splits only on whitespace.- When to use
Template. It is deliberately simpler than f-strings andstr.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 changedelimiter(default$) oridpatternto use another syntax. string.letters,string.maketransand other Python 2 names no longer exist; usestr.maketrans().
Related
str.format(),str.join(),re,textwrap- Lessons: String formatting, String methods