string
— Common string operations常见字符串操作¶
Source code: Lib/string.py
String constants字符串常量¶
The constants defined in this module are:本模块中定义的常量包括:
-
string.
ascii_letters
¶ The concatenation of the下面描述的ascii_lowercase
andascii_uppercase
constants described below.ascii_lowercase
和ascii_uppercase
常量的串联。This value is not locale-dependent.此值不依赖于语言环境。
-
string.
ascii_lowercase
¶ The lowercase letters小写字母'abcdefghijklmnopqrstuvwxyz'
.'abcdefghijklmnopqrstuvwxyz'
。This value is not locale-dependent and will not change.此值与语言环境无关,不会更改。
-
string.
ascii_uppercase
¶ The uppercase letters大写字母'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
。This value is not locale-dependent and will not change.此值与语言环境无关,不会更改。
-
string.
digits
¶ The string字符串'0123456789'
.'0123456789'
。
-
string.
hexdigits
¶ The string字符串'0123456789abcdefABCDEF'
.'0123456789abcdefABCDEF'
。
-
string.
octdigits
¶ The string字符串'01234567'
.'01234567'
。
-
string.
punctuation
¶ String of ASCII characters which are considered punctuation characters in the在C语言环境中被视为标点符号的ASCII字符字符串:C
locale:!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
.!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
。
-
string.
printable
¶ String of ASCII characters which are considered printable.可打印的ASCII字符字符串。This is a combination of这是digits
,ascii_letters
,punctuation
, andwhitespace
.digits
、ascii_letters
、punctuation
和whitespace
的组合。
-
string.
whitespace
¶ A string containing all ASCII characters that are considered whitespace.包含所有被视为空白的ASCII字符的字符串。This includes the characters space, tab, linefeed, return, formfeed, and vertical tab.这包括字符空间、制表符、换行符、回车符、换行符和垂直制表符。
Custom String Formatting自定义字符串格式¶
The built-in string class provides the ability to do complex variable substitutions and value formatting via the 内置的string类能够通过PEP 3101中描述的format()
method described in PEP 3101. format()
方法进行复杂的变量替换和值格式化。The Formatter
class in the string
module allows you to create and customize your own string formatting behaviors using the same implementation as the built-in format()
method.string
模块中的Formatter
类允许您使用与内置Formatter
方法相同的实现创建和自定义自己的字符串格式化行为。
-
class
string.
Formatter
¶ TheFormatter
class has the following public methods:Formatter
类具有以下公共方法:-
format
(format_string, /, *args, **kwargs)¶ The primary API method.主要的API方法。It takes a format string and an arbitrary set of positional and keyword arguments.它接受一个格式字符串和一组任意的位置参数和关键字参数。It is just a wrapper that calls它只是一个调用vformat()
.vformat()
的包装器。Changed in version 3.7:在3.7版中更改:A format string argument is now positional-only.格式字符串参数现在仅用于位置。
-
vformat
(format_string, args, kwargs)¶ This function does the actual work of formatting.此函数执行格式化的实际工作。It is exposed as a separate function for cases where you want to pass in a predefined dictionary of arguments, rather than unpacking and repacking the dictionary as individual arguments using the它作为一个单独的函数公开,用于传递预定义的参数字典,而不是使用*args
and**kwargs
syntax.*args
和**kwargs
语法将字典解包和重新打包为单个参数。vformat()
does the work of breaking up the format string into character data and replacement fields.将格式字符串分解为字符数据和替换字段。It calls the various methods described below.它调用下面描述的各种方法。
In addition, the此外,Formatter
defines a number of methods that are intended to be replaced by subclasses:Formatter
还定义了许多拟由子类替换的方法:-
parse
(format_string)¶ Loop over the format_string and return an iterable of tuples (literal_text, field_name, format_spec, conversion).循环使用format_字符串并返回一个元组(literable_text、field_name、format_spec、conversion)表。This is used byvformat()
to break the string into either literal text, or replacement fields.vformat()
使用它将字符串拆分为文本或替换字段。The values in the tuple conceptually represent a span of literal text followed by a single replacement field.元组中的值在概念上表示一个文本范围,后跟一个替换字段。If there is no literal text (which can happen if two replacement fields occur consecutively), then literal_text will be a zero-length string.如果没有文本(如果两个替换字段连续出现,可能会出现这种情况),那么literal_text将是一个长度为零的字符串。If there is no replacement field, then the values of field_name, format_spec and conversion will be如果没有替换字段,那么field_name、format_spec和conversion的值将为None
.None
。
-
get_field
(field_name, args, kwargs)¶ Given field_name as returned by给定parse()
(see above), convert it to an object to be formatted.parse()
返回的field_name(见上文),将其转换为要格式化的对象。Returns a tuple (obj, used_key).返回一个元组(obj, used_key)
。The default version takes strings of the form defined in PEP 3101, such as “0[name]” or “label.title”.默认版本采用PEP 3101中定义的格式的字符串,例如“0[name]
”或“label.title
”。args and kwargs are as passed in toarg和kwarg被传入vformat()
.vformat()
。The return value used_key has the same meaning as the key parameter toused_key的返回值与get_value()
.get_value()
的key参数具有相同的含义。
-
get_value
(key, args, kwargs)¶ Retrieve a given field value.检索给定的字段值。The key argument will be either an integer or a string.key参数将是整数或字符串。If it is an integer, it represents the index of the positional argument in args; if it is a string, then it represents a named argument in kwargs.如果是整数,则表示args中位置参数的索引;如果它是字符串,则表示kwargs中的命名参数。The args parameter is set to the list of positional arguments toargs参数设置为vformat()
, and the kwargs parameter is set to the dictionary of keyword arguments.vformat()
的位置参数列表,kwargs参数设置为关键字参数字典。For compound field names, these functions are only called for the first component of the field name; subsequent components are handled through normal attribute and indexing operations.对于复合字段名,仅对字段名的第一个组件调用这些函数;后续组件通过常规属性和索引操作进行处理。So for example, the field expression ‘0.name’ would cause例如,字段表达式“0name”会导致使用key参数0调用get_value()
to be called with a key argument of 0.get_value()
。Thename
attribute will be looked up afterget_value()
returns by calling the built-ingetattr()
function.get_value()
通过调用内置的getattr()
函数返回后,将查找name
属性。If the index or keyword refers to an item that does not exist, then an如果索引或关键字引用了不存在的项,则应引IndexError
orKeyError
should be raised.IndexError
或KeyError
。
-
check_unused_args
(used_args, args, kwargs)¶ Implement checking for unused arguments if desired.如果需要,实现对未使用参数的检查。The arguments to this function is the set of all argument keys that were actually referred to in the format string (integers for positional arguments, and strings for named arguments), and a reference to the args and kwargs that was passed to vformat.此函数的参数是格式字符串中实际引用的所有参数键(位置参数为整数,命名参数为字符串)的集合,以及对传递给vformat的args和kwargs的引用。The set of unused args can be calculated from these parameters.可以根据这些参数计算未使用的参数集。check_unused_args()
is assumed to raise an exception if the check fails.假设在检查失败时引发异常。
-
format_field
(value, format_spec)¶ format_field()
simply calls the global只需调用内置的全局format()
built-in.format()
。The method is provided so that subclasses can override it.提供该方法是为了子类可以重写它。
-
convert_field
(value, conversion)¶ Converts the value (returned by转换给定转换类型(如get_field()
) given a conversion type (as in the tuple returned by theparse()
method).parse()
方法返回的元组)的值(由get_field()
返回)。The default version understands ‘s’ (str), ‘r’ (repr) and ‘a’ (ascii) conversion types.默认版本理解“s”(str)、“r”(repr)和“a”(ascii)转换类型。
-
Format String Syntax格式字符串语法¶
The str.format()
method and the Formatter
class share the same syntax for format strings (although in the case of Formatter
, subclasses can define their own format string syntax). str.format()
方法和Formatter
类对于格式字符串共享相同的语法(尽管在Formatter
中,子类可以定义自己的格式字符串语法)。The syntax is related to that of formatted string literals, but it is less sophisticated and, in particular, does not support arbitrary expressions.该语法与格式化字符串文本的语法相关,但不太复杂,尤其不支持任意表达式。
Format strings contain “replacement fields” surrounded by curly braces 格式字符串包含由大括号{}
. {}
包围的“替换字段”。Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. 大括号中不包含的任何内容都被视为文字文本,它会原封不动地复制到输出中。If you need to include a brace character in the literal text, it can be escaped by doubling: 如果需要在文本中包含花括号字符,可以通过双花括号字符:{{
and }}
.{{
和}}
对其进行转义。
The grammar for a replacement field is as follows:替换字段的语法如下所示:
replacement_field ::= "{" [field_name
] ["!"conversion
] [":"format_spec
] "}"
field_name ::= arg_name ("."attribute_name
| "["element_index
"]")*
arg_name ::= [identifier
|digit
+]
attribute_name ::=identifier
element_index ::=digit
+ |index_string
index_string ::= <any source character except "]"> +
conversion ::= "r" | "s" | "a"
format_spec ::= <described in the next section>
In less formal terms, the replacement field can start with a field_name that specifies the object whose value is to be formatted and inserted into the output instead of the replacement field. 在不太正式的术语中,替换字段可以以field_name开头,该名称指定要格式化其值并插入到输出中的对象,而不是替换字段。The field_name is optionally followed by a conversion field, which is preceded by an exclamation point field_name后面有一个conversion字段,该字段前面有一个感叹号'!'
, and a format_spec, which is preceded by a colon ':'
. '!'
,还有一个format_spec,前面有一个冒号':'
。These specify a non-default format for the replacement value.它们为替换值指定非默认格式。
See also the Format Specification Mini-Language section.另请参见格式规范迷你语言部分。
The field_name itself begins with an arg_name that is either a number or a keyword. field_name本身以数字或关键字的arg_name开头。If it’s a number, it refers to a positional argument, and if it’s a keyword, it refers to a named keyword argument. 如果它是一个数字,它指的是一个位置参数,如果它是一个关键字,它指的是一个命名的关键字参数。If the numerical arg_names in a format string are 0, 1, 2, … in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, … will be automatically inserted in that order. 如果格式字符串中的数字arg_名称按顺序为0、1、2,则它们都可以省略(不仅仅是部分),数字0、1、2将自动按顺序插入。Because arg_name is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings 由于arg_name不以引号分隔,因此无法在格式字符串中指定任意字典键(例如字符串'10'
or ':-]'
) within a format string. '10'
或':-]'
)。The arg_name can be followed by any number of index or attribute expressions. arg_name后面可以跟任意数量的索引或属性表达式。An expression of the form 格式为'.name'
selects the named attribute using getattr()
, while an expression of the form '[index]'
does an index lookup using __getitem__()
.'.name'
的表达式使用getattr()
选择命名属性,而格式为'[index]'
的表达式使用__getitem__()
进行索引查找。
Changed in version 3.1: 在3.1版中更改:The positional argument specifiers can be omitted for str.format()
, so '{} {}'.format(a, b)
is equivalent to '{0} {1}'.format(a, b)
.str.format()
可以省略位置参数说明符,因此'{} {}'.format(a, b)
等同于'{0} {1}'.format(a, b)
。
Changed in version 3.4: 在3.4版中更改:The positional argument specifiers can be omitted for Formatter
.Formatter
程序可以省略位置参数说明符。
Some simple format string examples:一些简单的格式字符串示例:
"First, thou shalt count to {0}" # References first positional argument
"Bring me a {}" # Implicitly references the first positional argument
"From {} to {}" # Same as "From {0} to {1}"
"My quest is {name}" # References keyword argument 'name'
"Weight in tons {0.weight}" # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}" # First element of keyword argument 'players'.
The conversion field causes a type coercion before formatting. conversion在格式化之前会导致类型强制。Normally, the job of formatting a value is done by the 通常,格式化一个值的工作是由值本身的__format__()
method of the value itself. __format__()
方法完成的。However, in some cases it is desirable to force a type to be formatted as a string, overriding its own definition of formatting. 但是,在某些情况下,最好强制将类型格式化为字符串,从而覆盖其自身的格式定义。By converting the value to a string before calling 通过在调用__format__()
, the normal formatting logic is bypassed.__format__()
之前将值转换为字符串,可以绕过正常的格式化逻辑。
Three conversion flags are currently supported: 当前支持三个转换标志:'!s'
which calls str()
on the value, '!r'
which calls repr()
and '!a'
which calls ascii()
.'!s'
对值调用str()
,'!r'
调用repr()
而'!a'
调用ascii()
。
Some examples:例如:
"Harold's a clever {0!s}" # Calls str() on the argument first
"Bring out the holy {name!r}" # Calls repr() on the argument first
"More {!a}" # Calls ascii() on the argument first
The format_spec field contains a specification of how the value should be presented, including such details as field width, alignment, padding, decimal precision and so on. format_spec字段包含一个值应该如何显示的规范,包括字段宽度、对齐、填充、小数精度等细节。Each value type can define its own “formatting mini-language” or interpretation of the format_spec.每个值类型都可以定义自己的“格式迷你语言”或对format_spec的解释。
Most built-in types support a common formatting mini-language, which is described in the next section.大多数内置类型都支持通用格式迷你语言,这将在下一节中介绍。
A format_spec field can also include nested replacement fields within it. format_spec字段也可以包含嵌套的替换字段。These nested replacement fields may contain a field name, conversion flag and format specification, but deeper nesting is not allowed. 这些嵌套替换字段可能包含字段名、转换标志和格式规范,但不允许更深的嵌套。The replacement fields within the format_spec are substituted before the format_spec string is interpreted. 在解释format_spec字符串之前,将替换format_spec中的替换字段。This allows the formatting of a value to be dynamically specified.这允许动态指定值的格式。
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). “格式规范”在格式字符串中包含的替换字段中使用,以定义各个值的显示方式(请参见格式字符串语法和格式字符串文字)。They can also be passed directly to the built-in 它们也可以直接传递给内置的format()
function. format()
函数。Each formattable type may define how the format specification is to be interpreted.每个formattable类型都可以定义如何解释格式规范。
Most built-in types implement the following options for format specifications, although some of the formatting options are only supported by the numeric types.大多数内置类型为格式规范实现以下选项,尽管一些格式选项仅由数字类型支持。
A general convention is that an empty format specification produces the same result as if you had called 一般的约定是,空格式规范产生的结果与对值调用str()
on the value. str()
时的结果相同。A non-empty format specification typically modifies the result.非空格式规范通常会修改结果。
The general form of a standard format specifier is:标准格式说明符的一般形式是:
format_spec ::= [[fill
]align
][sign
][#][0][width
][grouping_option
][.precision
][type
]
fill ::= <any character>
align ::= "<" | ">" | "=" | "^"
sign ::= "+" | "-" | " "
width ::=digit
+
grouping_option ::= "_" | ","
precision ::=digit
+
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
If a valid align value is specified, it can be preceded by a fill character that can be any character and defaults to a space if omitted. 如果指定了有效的align值,则该值前面可以有一个fill字符,该字符可以是任何字符,如果省略,则默认为空格。It is not possible to use a literal curly brace (“在格式化字符串文本中或使用{
” or “}
”) as the fill character in a formatted string literal or when using the str.format()
method. str.format()
方法时,不可能使用文本大括号(“{
”或“}
”)作为fill字符。However, it is possible to insert a curly brace with a nested replacement field. 但是,可以插入带有嵌套替换字段的大括号。This limitation doesn’t affect the 此限制不影响format()
function.format()
函数。
The meaning of the various alignment options is as follows:各种路线选项的含义如下:
Option选项
Meaning意思
'<'
Forces the field to be left-aligned within the available space (this is the default for most objects).强制字段在可用空间内左对齐(这是大多数对象的默认设置)。
'>'
Forces the field to be right-aligned within the available space (this is the default for numbers).强制字段在可用空间内右对齐(这是数字的默认值)。
'='
Forces the padding to be placed after the sign (if any) but before the digits.强制将填充放置在符号(如果有)之后但数字之前。This is used for printing fields in the form ‘+000000120’.这用于打印格式为“+000000120”的字段。This alignment option is only valid for numeric types.此对齐选项仅对数字类型有效。It becomes the default for numbers when ‘0’ immediately precedes the field width.当“0”紧跟在字段宽度之前时,它将成为数字的默认值。
'^'
Forces the field to be centered within the available space.强制字段在可用空间内居中。
Note that unless a minimum field width is defined, the field width will always be the same size as the data to fill it, so that the alignment option has no meaning in this case.请注意,除非定义了最小字段宽度,否则字段宽度将始终与填充数据的大小相同,因此在这种情况下,“对齐”选项没有任何意义。
The sign option is only valid for number types, and can be one of the following:sign选项仅对数字类型有效,可以是以下类型之一:
Option选项
Meaning意思
'+'
indicates that a sign should be used for both positive as well as negative numbers.指示正负数字都应使用符号。
'-'
indicates that a sign should be used only for negative numbers (this is the default behavior).指示符号应仅用于负数(这是默认行为)。space
indicates that a leading space should be used on positive numbers, and a minus sign on negative numbers.指示正数应使用前导空格,负数应使用负号。
The '#'
option causes the “alternate form” to be used for the conversion. '#'
选项导致转换使用“替代形式”。The alternate form is defined differently for different types. 对于不同的类型,备用形式的定义不同。This option is only valid for integer, float and complex types. 此选项仅对整数、浮点和复杂类型有效。For integers, when binary, octal, or hexadecimal output is used, this option adds the respective prefix 对于整数,当使用二进制、八进制或十六进制输出时,此选项会将相应的前缀'0b'
, '0o'
, '0x'
, or '0X'
to the output value. '0b'
、'0o'
、'0x'
或'0X'
添加到输出值。For float and complex the alternate form causes the result of the conversion to always contain a decimal-point character, even if no digits follow it. 对于float和complex,交替形式会导致转换结果始终包含小数点字符,即使后面没有数字。Normally, a decimal-point character appears in the result of these conversions only if a digit follows it. 通常,只有在转换结果后面有数字时,才会在转换结果中显示小数点字符。In addition, for 此外,对于'g'
and 'G'
conversions, trailing zeros are not removed from the result.'g'
和'G'
转换,不会从结果中删除尾随零。
The ','
option signals the use of a comma for a thousands separator. ','
选项表示使用逗号作为千位分隔符。For a locale aware separator, use the 对于区域设置感知分隔符,请改用'n'
integer presentation type instead.'n'
整数表示类型。
The '_'
option signals the use of an underscore for a thousands separator for floating point presentation types and for integer presentation type 'd'
. '_'
选项表示在浮点表示类型和整数表示类型“d”中使用下划线作为千位分隔符。For integer presentation types 对于整数表示类型'b'
, 'o'
, 'x'
, and 'X'
, underscores will be inserted every 4 digits. 'b'
、'o'
、'x'
和'X'
,将每隔4位插入下划线。For other presentation types, specifying this option is an error.对于其他演示文稿类型,指定此选项是错误的。
width is a decimal integer defining the minimum total field width, including any prefixes, separators, and other formatting characters. width是定义最小总字段宽度的十进制整数,包括任何前缀、分隔符和其他格式字符。If not specified, then the field width will be determined by the content.如果未指定,则字段宽度将由内容决定。
When no explicit alignment is given, preceding the width field by a zero (如果没有给出明确的对齐方式,则在width字段前面加一个零('0'
) character enables sign-aware zero-padding for numeric types. '0'
)字符可以为数字类型启用符号感知的零填充。This is equivalent to a fill character of 这相当于alignment类型为'0'
with an alignment type of '='
.'='
的fill字符'0'
。
Changed in version 3.10:版本3.10中更改: Preceding the width field by '0'
no longer affects the default alignment for strings.'0'
前面的width字段不再影响字符串的默认对齐方式。
The precision is a decimal integer indicating how many digits should be displayed after the decimal point for presentation types precision是一个十进制整数,表示在表示类型'f'
and 'F'
, or before and after the decimal point for presentation types 'g'
or 'G'
. 'f'
和'F'
的小数点之后,或在表示类型'g'
或'G'
的小数点之前和之后,应显示多少位数。For string presentation types the field indicates the maximum field size - in other words, how many characters will be used from the field content. 对于字符串表示类型,字段指示最大字段大小-换句话说,将使用字段内容中的多少个字符。The precision is not allowed for integer presentation types.整数表示类型不允许precision。
Finally, the type determines how the data should be presented.最后,type决定了数据的显示方式。
The available string presentation types are:可用的字符串表示形式有:
Type类型
Meaning意思
's'
String format.字符串格式。This is the default type for strings and may be omitted.这是字符串的默认类型,可以省略。None
The same as
's'
.
The available integer presentation types are:可用的整数表示类型有:
Type类型
Meaning意思
'b'
Binary format.二进制格式。Outputs the number in base 2.输出基数为2的数字。
'c'
Character.字符。Converts the integer to the corresponding unicode character before printing.打印前将整数转换为相应的unicode字符。
'd'
Decimal Integer.十进制整数。Outputs the number in base 10.输出以10为基数的数字。
'o'
Octal format.八进制格式。Outputs the number in base 8.输出基数为8的数字。
'x'
Hex format.十六进制格式。Outputs the number in base 16, using lower-case letters for the digits above 9.输出以16为基数的数字,对9以上的数字使用小写字母。
'X'
Hex format.十六进制格式。Outputs the number in base 16, using upper-case letters for the digits above 9.输出以16为基数的数字,对9以上的数字使用大写字母。In case如果指定了'#'
is specified, the prefix'0x'
will be upper-cased to'0X'
as well.'#'
,前缀'0x'
也将大写为'0X'
。
'n'
Number.数字。This is the same as这与'd'
, except that it uses the current locale setting to insert the appropriate number separator characters.'d'
相同,只是它使用当前的区域设置插入适当的数字分隔符。None
The same as与'd'
.'d'
相同。
In addition to the above presentation types, integers can be formatted with the floating point presentation types listed below (except 除上述表示类型外,整数还可以使用下面列出的浮点表示类型进行格式化('n'
and None
). 'n'
和None
除外)。When doing so, 进行此操作时,float()
is used to convert the integer to a floating point number before formatting.float()
用于在格式化之前将整数转换为浮点数。
The available presentation types for float
and Decimal
values are:float
和Decimal
值的可用表示形式有:
Type类型
Meaning意思
'e'
Scientific notation.科学记数法。For a given precision对于给定的精度p
, formats the number in scientific notation with the letter ‘e’ separating the coefficient from the exponent.p
,用字母“e”将系数与指数分开,以科学记数法格式化数字。The coefficient has one digit before and系数在小数点前有一个数字,小数点后有p
digits after the decimal point, for a total ofp + 1
significant digits.p
个数字,总共有p+1
个有效数字。With no precision given, uses a precision of在没有给定精度的情况下,在6
digits after the decimal point forfloat
, and shows all coefficient digits forDecimal
.float
小数点后使用6
位精度,并显示十进制的所有系数位数。If no digits follow the decimal point, the decimal point is also removed unless the如果小数点后没有数字,则除非使用#
option is used.#
选项,否则也会删除小数点。
'E'
Scientific notation.科学记数法。Same as与'e'
except it uses an upper case ‘E’ as the separator character.'e'
相同,只是它使用大写“E”作为分隔符。
'f'
Fixed-point notation.定点符号。For a given precision对于给定的精度p
, formats the number as a decimal number with exactlyp
digits following the decimal point.p
,将数字格式化为小数点后紧跟着p位的十进制数。With no precision given, uses a precision of如果没有给定精度,则在6
digits after the decimal point forfloat
, and uses a precision large enough to show all coefficient digits forDecimal
.float
小数点后使用6
位数的精度,并使用足够大的精度来显示Decimal
的所有系数位数。If no digits follow the decimal point, the decimal point is also removed unless the如果小数点后没有数字,则除非使用#
option is used.#
选项,否则也会删除小数点。
'F'
Fixed-point notation.定点符号。Same as与'f'
, but convertsnan
toNAN
andinf
toINF
.'f'
相同,但将nan
转换为NAN
,将inf
转换为INF
。
'g'
General format.通用格式。For a given precision对于给定的精度p >= 1
, this rounds the number top
significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.p > = 1
,这会将数字四舍五入为p
个有效数字,然后根据其大小将结果格式化为定点格式或科学记数法。A precision of精度0
is treated as equivalent to a precision of1
.0
被视为等同于精度1
。
The precise rules are as follows: suppose that the result formatted with presentation type精确规则如下:假设使用表示类型'e'
and precisionp-1
would have exponentexp
.'e'
和精度p-1
格式化的结果将具有指数exp
。Then, if然后,如果m <= exp < p
, wherem
is -4 for floats and -6 forDecimals
, the number is formatted with presentation type'f'
and precisionp-1-exp
.m <= exp < p
,其中m
表示浮点数为-4,Decimals
位数为-6,则数字的格式为表示类型'f'
,精度为p-1-exp
。Otherwise, the number is formatted with presentation type否则,数字的格式为表示类型'e'
and precisionp-1
.'e'
和精度p-1
。In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it, unless the在这两种情况下,都会从有效位中删除不重要的尾随零,如果后面没有剩余的数字,则也会删除小数点,除非使用'#'
option is used.'#'
选项。
With no precision given, uses a precision of在没有给定精度的情况下,对6
significant digits forfloat
.float
使用6
位有效数字的精度。For对于Decimal
, the coefficient of the result is formed from the coefficient digits of the value; scientific notation is used for values smaller than1e-6
in absolute value and values where the place value of the least significant digit is larger than 1, and fixed-point notation is used otherwise.Decimal
,结果的系数由值的系数位数组成;科学记数法用于绝对值小于1e-6
的值,以及最低有效位的位置值大于1的值,否则使用定点记数法。
Positive and negative infinity, positive and negative zero, and nans, are formatted as无论精度如何,正无穷大和负无穷大、正零和负零以及nan的格式分别为inf
,-inf
,0
,-0
andnan
respectively, regardless of the precision.inf
、-inf
、0
、-0
和nan
。
'G'
General format.通用格式。Same as与'g'
except switches to'E'
if the number gets too large.'g'
相同,但如果数字太大,则切换到'E'
。The representations of infinity and NaN are uppercased, too.无穷大和NaN的表示也是大写的。
'n'
Number.数字。This is the same as这与'g'
, except that it uses the current locale setting to insert the appropriate number separator characters.'g'
相同,只是它使用当前区域设置插入适当的数字分隔符。
'%'
Percentage.百分率。Multiplies the number by 100 and displays in fixed (将数字乘以100,并以固定('f'
) format, followed by a percent sign.'f'
)格式显示,后跟百分号。
None
For对于float
this is the same as'g'
, except that when fixed-point notation is used to format the result, it always includes at least one digit past the decimal point.float
,这与'g'
相同,只是当使用定点表示法格式化结果时,它总是至少包含一个超过小数点的数字。The precision used is as large as needed to represent the given value faithfully.所使用的精度与准确表示给定值所需的精度一样大。
For对于Decimal
, this is the same as either'g'
or'G'
depending on the value ofcontext.capitals
for the current decimal context.Decimal
,这与'g'
或'G'
相同,具体取决于当前十进制上下文的context.capitals
值。
The overall effect is to match the output of总体效果是匹配由其他格式修饰符更改的str()
as altered by the other format modifiers.str()
的输出。
Format examples设置示例格式¶
This section contains examples of the 本节包含str.format()
syntax and comparison with the old %
-formatting.str.format()
语法的示例以及与旧%
格式的比较。
In most of the cases the syntax is similar to the old 在大多数情况下,语法与旧的%
-formatting, with the addition of the {}
and with :
used instead of %
. %
格式相似,添加了{}
和:
代替%
。For example, 例如,'%03.2f'
can be translated to '{:03.2f}'
.'%03.2f'
可以翻译为'{:03.2f}'
。
The new format syntax also supports new and different options, shown in the following examples.新格式语法还支持新的和不同的选项,如以下示例所示。
Accessing arguments by position:按位置访问参数:
>>> '{0}, {1}, {2}'.format('a', 'b', 'c')
'a, b, c'
>>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+ only
'a, b, c'
>>> '{2}, {1}, {0}'.format('a', 'b', 'c')
'c, b, a'
>>> '{2}, {1}, {0}'.format(*'abc') # unpacking argument sequence
'c, b, a'
>>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated
'abracadabra'
Accessing arguments by name:按名称访问参数:
>>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
'Coordinates: 37.24N, -115.81W'
>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
>>> 'Coordinates: {latitude}, {longitude}'.format(**coord)
'Coordinates: 37.24N, -115.81W'
Accessing arguments’ attributes:访问参数的属性:
>>> c = 3-5j
>>> ('The complex number {0} is formed from the real part {0.real} '
... 'and the imaginary part {0.imag}.').format(c)
'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.'
>>> class Point:
... def __init__(self, x, y):
... self.x, self.y = x, y
... def __str__(self):
... return 'Point({self.x}, {self.y})'.format(self=self)
...
>>> str(Point(4, 2))
'Point(4, 2)'
Accessing arguments’ items:访问参数项:
>>> coord = (3, 5)
>>> 'X: {0[0]}; Y: {0[1]}'.format(coord)
'X: 3; Y: 5'
Replacing 替换%s
and %r
:%s
和%r
:
>>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')
"repr() shows quotes: 'test1'; str() doesn't: test2"
Aligning the text and specifying a width:对齐文本并指定宽度:
>>> '{:<30}'.format('left aligned')
'left aligned '
>>> '{:>30}'.format('right aligned')
' right aligned'
>>> '{:^30}'.format('centered')
' centered '
>>> '{:*^30}'.format('centered') # use '*' as a fill char
'***********centered***********'
Replacing 替换%+f
, %-f
, and % f
and specifying a sign:%+f
、%-f
和% f
并指定符号:
>>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it always
'+3.140000; -3.140000'
>>> '{: f}; {: f}'.format(3.14, -3.14) # show a space for positive numbers
' 3.140000; -3.140000'
>>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the minus -- same as '{:f}; {:f}'
'3.140000; -3.140000'
Replacing 替换%x
and %o
and converting the value to different bases:%x
和%o
并将值转换为不同的基数:
>>> # format also supports binary numbers
>>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42)
'int: 42; hex: 2a; oct: 52; bin: 101010'
>>> # with 0x, 0o, or 0b as prefix:
>>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42)
'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'
Using the comma as a thousands separator:使用逗号作为千位分隔符:
>>> '{:,}'.format(1234567890)
'1,234,567,890'
Expressing a percentage:表示百分比:
>>> points = 19
>>> total = 22
>>> 'Correct answers: {:.2%}'.format(points/total)
'Correct answers: 86.36%'
Using type-specific formatting:使用特定于类型的格式:
>>> import datetime
>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
'2010-07-04 12:15:58'
Nesting arguments and more complex examples:嵌套参数和更复杂的示例:
>>> for align, text in zip('<^>', ['left', 'center', 'right']):
... '{0:{fill}{align}16}'.format(text, fill=align, align=align)
...
'left<<<<<<<<<<<<'
'^^^^^center^^^^^'
'>>>>>>>>>>>right'
>>>
>>> octets = [192, 168, 0, 1]
>>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)
'C0A80001'
>>> int(_, 16)
3232235521
>>>
>>> width = 5
>>> for num in range(5,12):
... for base in 'dXob':
... print('{0:{width}{base}}'.format(num, base=base, width=width), end=' ')
... print()
...
5 5 5 101
6 6 6 110
7 7 7 111
8 8 10 1000
9 9 11 1001
10 A 12 1010
11 B 13 1011
Template strings模板字符串¶
Template strings provide simpler string substitutions as described in PEP 292. 模板字符串提供了更简单的字符串替换,如PEP 292所述。A primary use case for template strings is for internationalization (i18n) since in that context, the simpler syntax and functionality makes it easier to translate than other built-in string formatting facilities in Python. 模板字符串的一个主要用例是国际化(i18n),因为在这种情况下,与Python中的其他内置字符串格式化工具相比,更简单的语法和功能使其更易于翻译。As an example of a library built on template strings for i18n, see the flufl.i18n package.作为基于i18n模板字符串构建的库的示例,请参阅flufl.i18n包。
Template strings support 模板字符串支持基于$
-based substitutions, using the following rules:$
的替换,使用以下规则:
$$
is an escape; it is replaced with a single是一种转义;它被替换为一个$
.$
。$identifier
names a substitution placeholder matching a mapping key of命名与映射键"identifier"
."identifier"
匹配的替换占位符。By default,默认情况下,"identifier"
is restricted to any case-insensitive ASCII alphanumeric string (including underscores) that starts with an underscore or ASCII letter."identifier"
仅限于以下划线或ASCII字母开头的任何不区分大小写的ASCII字母数字字符串(包括下划线)。The first non-identifier character after the$
character terminates this placeholder specification.$
字符后的第一个非标识符字符终止此占位符规范。${identifier}
is equivalent to等同于$identifier
.$identifier
。It is required when valid identifier characters follow the placeholder but are not part of the placeholder, such as如果有效的标识符字符位于占位符之后,但不是占位符的一部分,例如"${noun}ification"
."${noun}ification"
,则需要此选项。
Any other appearance of 字符串中出现的任何其他$
in the string will result in a ValueError
being raised.$
将导致引发ValueError
。
The string
module provides a Template
class that implements these rules. string
模块提供了一个实现这些规则的Template
类。The methods of Template
are:Template
的方法有:
-
class
string.
Template
(template)¶ The constructor takes a single argument which is the template string.构造函数接受单个参数,即模板字符串。-
substitute
(mapping={}, /, **kwds)¶ Performs the template substitution, returning a new string.执行模板替换,返回新字符串。mappingis any dictionary-like object with keys that match the placeholders in the template.是任何类似字典的对象,其键与模板中的占位符匹配。Alternatively, you can provide keyword arguments, where the keywords are the placeholders.或者,您可以提供关键字参数,其中关键字是占位符。When both mapping and kwds are given and there are duplicates, the placeholders from kwds take precedence.当同时给出mapping和kwds并且存在重复项时,kwds中的占位符优先。
-
safe_substitute
(mapping={}, /, **kwds)¶ Like与substitute()
, except that if placeholders are missing from mapping and kwds, instead of raising aKeyError
exception, the original placeholder will appear in the resulting string intact.substitute()
类似,但如果mapping和kwds中缺少占位符,则原始占位符将原封不动地显示在结果字符串中,而不是引发KeyError
异常。Also, unlike with此外,与substitute()
, any other appearances of the$
will simply return$
instead of raisingValueError
.substitute()
不同,$
的任何其他外观都将返回$
,而不是引发ValueError
。While other exceptions may still occur, this method is called “safe” because it always tries to return a usable string instead of raising an exception.虽然仍可能发生其他异常,但此方法称为“安全”,因为它总是尝试返回可用字符串,而不是引发异常。In another sense,从另一个意义上讲,safe_substitute()
may be anything other than safe, since it will silently ignore malformed templates containing dangling delimiters, unmatched braces, or placeholders that are not valid Python identifiers.safe_substitute()
可能不安全,因为它会自动忽略格式错误的模板,这些模板包含悬空分隔符、不匹配的大括号或占位符,这些都是无效的Python标识符。
Template
instances also provide one public data attribute:实例还提供一个公共数据属性:-
template
¶ This is the object passed to the constructor’s template argument.这是传递给构造函数的template参数的对象。In general, you shouldn’t change it, but read-only access is not enforced.一般来说,您不应该更改它,但不会强制执行只读访问。
-
Here is an example of how to use a Template:以下是如何使用模板的示例:
>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
>>> d = dict(who='tim')
>>> Template('Give $who $100').substitute(d)
Traceback (most recent call last):
...
ValueError: Invalid placeholder in string: line 1, col 11
>>> Template('$who likes $what').substitute(d)
Traceback (most recent call last):
...
KeyError: 'what'
>>> Template('$who likes $what').safe_substitute(d)
'tim likes $what'
Advanced usage: you can derive subclasses of 高级用法:您可以派生Template
to customize the placeholder syntax, delimiter character, or the entire regular expression used to parse template strings. Template
的子类来自定义占位符语法、分隔符或用于分析模板字符串的整个正则表达式。To do this, you can override these class attributes:为此,可以重写这些类属性:
delimiter –
This is the literal string describing a placeholder introducing delimiter.这是描述引入分隔符的占位符的文本字符串。The default value is默认值为$
.$
。Note that this should not be a regular expression, as the implementation will call请注意,这不应该是正则表达式,因为实现将根据需要对此字符串调用re.escape()
on this string as needed.re.escape()
。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]*)
.(?a:[_a-z][_a-z0-9]*)
。If this is given and braceidpattern is如果给定了此模式,且braceidpattern为None
this pattern will also apply to braced placeholders.None
,则此模式也将应用于括号占位符。Note
Since default flags is由于默认flags是re.IGNORECASE
, pattern[a-z]
can match with some non-ASCII characters.re.IGNORECASE
,所以模式[a-z]
可以与一些非ASCII字符匹配。That’s why we use the local这就是为什么我们在这里使用本地a
flag here.a
标志。Changed in version 3.7:版本3.7中更改: braceidpatterncan be used to define separate patterns used inside and outside the braces.可用于定义大括号内外使用的单独图案。braceidpattern –
This is like idpattern but describes the pattern for braced placeholders.这类似于idpattern,但描述了带括号占位符的模式。Defaults to默认为None
which means to fall back to idpattern (i.e. the same pattern is used both inside and outside braces).None
,这意味着返回到idpattern(即在大括号内和大括号外使用相同的模式)。If given, this allows you to define different patterns for braced and unbraced placeholders.如果给定,这允许您为带支撑和不带支撑的占位符定义不同的模式。New in version 3.7.版本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
.re.IGNORECASE
。Note that请注意,re.VERBOSE
will always be added to the flags, so custom idpatterns must follow conventions for verbose regular expressions.re.VERBOSE
将始终添加到标志中,因此自定义idpattern必须遵循详细正则表达式的约定。New in version 3.2.版本3.2中新增。
Alternatively, you can provide the entire regular expression pattern by overriding the class attribute pattern. 或者,可以通过重写类属性pattern来提供整个正则表达式模式。If you do this, the value must be a regular expression object with four named capturing groups. 如果执行此操作,则该值必须是具有四个命名捕获组的正则表达式对象。The capturing groups correspond to the rules given above, along with the invalid placeholder rule:捕获组与上述规则以及无效占位符规则相对应:
escaped –
This group matches the escape sequence, e.g.此组匹配默认模式中的转义序列,例如$$
, in the default pattern.$$
。named –
This group matches the unbraced placeholder name; it should not include the delimiter in capturing group.该组匹配无支撑占位符名称;捕获组中不应包含分隔符。braced –
This group matches the brace enclosed placeholder name; it should not include either the delimiter or braces in the capturing group.此组与大括号内的占位符名称匹配;捕获组中不应包含分隔符或大括号。invalid –
This group matches any other delimiter pattern (usually a single delimiter), and it should appear last in the regular expression.该组与任何其他分隔符模式(通常是单个分隔符)匹配,并且应该在正则表达式中最后出现。
Helper functions辅助函数¶
-
string.
capwords
(s, sep=None)¶ Split the argument into words using使用str.split()
, capitalize each word usingstr.capitalize()
, and join the capitalized words usingstr.join()
.str.split()
将参数拆分为单词,使用str.capitalize()
将每个单词大写,并使用str.join()
将大写的单词连接起来。If the optional second argument sep is absent or如果可选的第二个参数sep不存在或None
, runs of whitespace characters are replaced by a single space and leading and trailing whitespace are removed, otherwise sep is used to split and join the words.None
,则将用单个空格替换一行空格字符,并删除前导和尾随空格,否则使用sep拆分和连接单词。