Skip to main content

string

Character class constants, a Formatter extension point for str.format, and Template for $identifier substitution.

Source-of-record: Lib/string.py, string docs.

Character classes

ConstantValue
ascii_lowercase'abcdefghijklmnopqrstuvwxyz'.
ascii_uppercase'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
ascii_lettersConcatenation of the two above.
digits'0123456789'.
hexdigits'0123456789abcdefABCDEF'.
octdigits'01234567'.
punctuationASCII punctuation.
whitespaceSpace, tab, newline, return, formfeed, vertical tab.
printableDigits + letters + punctuation + whitespace.

Formatter

Backbone of str.format / f-strings. Override to customise.

MethodOverride role
format(format_string, *args, **kwargs)Top-level entry.
vformat(format_string, args, kwargs)Bypass packing.
parse(format_string)Iterator of (literal, field_name, spec, conversion).
get_field(field_name, args, kwargs)Resolve 0.attr[key].
get_value(key, args, kwargs)Look up by name or position.
check_unused_args(used_args, args, kwargs)Validation hook.
format_field(value, format_spec)Apply format spec.
convert_field(value, conversion)Apply !s, !r, !a.

Template

Template performs $identifier and ${identifier} substitution without invoking str.format's richer mini-language.

MethodEffect
Template(template)Construct.
substitute(mapping={}, /, **kwargs)Replace; missing keys raise KeyError.
safe_substitute(mapping={}, /, **kwargs)Leave missing placeholders intact.
get_identifiers()Names referenced (3.11+).
is_valid()Predicate (3.11+).

Customise via Template.delimiter, Template.idpattern, Template.braceidpattern, Template.flags.

capwords

string.capwords(s, sep=None) splits on whitespace and capitalises each word. Equivalent to sep.join(x.capitalize() for x in s.split(sep)).

Gopy status

AreaState
All character class constantsComplete.
Formatter APIComplete.
Template (incl. is_valid)Complete.
capwordsComplete.

Reference

  • CPython 3.14: string.
  • Lib/string.py.
  • module/string/, module/_string/. gopy port.