string — Common string operations¶
Source code: Lib/string.py
See also.
punctuation¶
String of ASCII characters which are considered punctuation characters in the
Clocale.
string.
printable¶
String of ASCII characters which are considered printable. This is a combination of
digits,
ascii_letters,
punctuation, and
whitespace.
Custom String Formatting¶.
- class
string.
Formatter¶
The
Formatterclass has the following public methods:
format(format_string, *args, **kwargs)¶
The primary API method. It takes a format string and an arbitrary set of positional and keyword arguments. It is just a wrapper that calls
vformat().
Changed in version 3.7: A format string argument is now positional-only. ‘s’ (str), ‘r’ (repr) and ‘a’ (ascii) conversion types.|
digit+] attribute_name ::=
identifierelement_index ::=
digit+ |
index_stringindex for
str.format(),
so
'{} {}'.format(a, b) is equivalent to
'{0} {1}'.format(a,.
See the Format examples section for some examples.
Format Specification Mini-Language¶
“Format specifications” are used within replacement fields contained within a
format string to define how individual values are presented (see
Format String Syntax and Formatted string literals).][
grouping_option][.
precision][
type] fill ::= <any character> align ::= "<" | ">" | "=" | "^" sign ::= "+" | "-" | " " width ::=
digit+ grouping_option ::= "_" | "," precision ::=
digit+ type ::=  in a formatted string literal or when using the
str.format()
method. However, it is possible to insert a curly brace
with a nested replacement field. This limitation doesn’t
affect the
format() function..
The
'_' option signals the use of an underscore for a thousands
separator for floating point presentation types and for integer
presentation type
'd'. For integer presentation types
'b',
'o',
'x', and
'X', underscores will be inserted every 4
digits. For other presentation types, specifying this option is an
error.
width is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content.
When no explicit alignment is given,:
Format examples¶
This section contains examples of the
str following
Template strings¶. As an example of a library built on template strings for i18n, see the flufl.i18n package.
Template strings support
$-based substitutions, using the following rules:
$$is an escape; it is replaced with a single
$.
$identifiernames a substitution placeholder matching a mapping key of
"identifier". By default,
"identifier"is restricted to any case-insensitive ASCII alphanumeric string (including underscores) that starts with an underscore or ASCII letter.wds)¶.
safe_substitute(mapping, **kwds)¶
Like
substitute(), except that if placeholders are missing from mapping and kwds, 11 >>>. Note further that you cannot change the delimiter after class creation (i.e. a different delimiter must be set in the subclass’s class namespace).
idpattern – This is the regular expression describing the pattern for non-braced placeholders. The default value is the regular expression
(?a:[_a-z][_a-z0-9]*). If this is given and braceidpattern is
Nonethis pattern will also apply to braced placeholders.
Note
Since default flags is
re.IGNORECASE, pattern
[a-z]can match with some non-ASCII characters. That’s why we use the local
aflag here.
Changed in version 3.7: braceidpattern can be used to define separate patterns used inside and outside the braces.
braceidpattern – This is like idpattern but describes the pattern for braced placeholders. Defaults to
Nonewhich means to fall back to idpattern (i.e. the same pattern is used both inside and outside braces). If given, this allows you to define different patterns for braced and unbraced placeholders.
New in version 3.7.
flags – The regular expression flags that will be applied when compiling the regular expression used for recognizing substitutions. The default value is
re.IGNORECASE. Note that
re.VERBOSEwill:
-.
Helper functions¶
string.
capwords(s, sep=None)¶.