python处理字符串获取其后六位_string --- 常见的字符串操作 — Python 3.8.6 文档

格式规格迷你语言¶

“格式规格”在格式字符串所包含的替换字段内部使用,用于定义单个值应如何呈现 (参见 格式字符串语法 和 格式化字符串字面值)。 它们也可以被直接传给内置的 format() 函数。 每种可格式化的类型都可以自行定义如何对格式规格进行解读。

大多数内置类型都为格式规格实现了下列选项,不过某些格式化选项只被数值类型所支持。

一般约定空的格式描述将产生与在值上调用 str() 相同的结果。 非空格式描述通常会修改此结果。

标准格式说明符 的一般形式如下:

format_spec ::= [[fill]align][sign][#][0][width][grouping_option][.precision][type]

fill ::=

align ::= "" | "=" | "^"

sign ::= "+" | "-" | " "

width ::= digit+

grouping_option ::= "_" | ","

precision ::= digit+

type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"

如果指定了一个有效的 align 值,则可以在该值前面加一个 fill 字符,它可以为任意字符,如果省略则默认为空格符。 在 格式化字符串字面值 或在使用 str.format() 方法时是无法使用花括号字面值 ("{" or "}") 作为 fill 字符的。 但是,通过嵌套替换字段插入花括号则是可以的。 这个限制不会影响 format() 函数。

各种对齐选项的含义如下:

选项

含义

'

强制字段在可用空间内左对齐(这是大多数对象的默认值)。

'>'

强制字段在可用空间内右对齐(这是数字的默认值)。

'='

强制将填充放置在符号(如果有)之后但在数字之前。这用于以“+000000120”形式打印字段。此对齐选项仅对数字类型有效。当'0'紧接在字段宽度之前时,它成为默认值。

'^'

强制字段在可用空间内居中。

请注意,除非定义了最小字段宽度,否则字段宽度将始终与填充它的数据大小相同,因此在这种情况下,对齐选项没有意义。

sign 选项仅对数字类型有效,可以是以下之一:

选项

含义

'+'

表示标志应该用于正数和负数。

'-'

表示标志应仅用于负数(这是默认行为)。

space

表示应在正数上使用前导空格,在负数上使用减号。

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 prefix respective '0b', '0o', or

'0x' to the output value. 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. 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.

',' 选项表示使用逗号作为千位分隔符。 对于感应区域设置的分隔符,请改用 'n' 整数表示类型。

在 3.1 版更改:添加了 ',' 选项 (另请参阅 PEP 378)。

'_' 选项表示对浮点表示类型和整数表示类型 'd' 使用下划线作为千位分隔符。 对于整数表示类型 'b', 'o', 'x' 和 'X',将为每 4 个数位插入一个下划线。 对于其他表示类型指定此选项则将导致错误。

在 3.6 版更改:添加了 '_' 选项 (另请参阅 PEP 515)。

width 是一个定义最小总字段宽度的十进制整数,包括任何前缀、分隔符和其他格式化字符。 如果未指定,则字段宽度将由内容确定。

当未显式给出对齐方式时,在 width 字段前加一个零 ('0') 字段将为数字类型启用感知正负号的零填充。 这相当于设置 fill 字符为 '0' 且 alignment 类型为 '='。

precision 是一个十进制数字,表示对于以 'f' and 'F' 格式化的浮点数值要在小数点后显示多少个数位,或者对于以 'g' 或 'G' 格式化的浮点数值要在小数点前后共显示多少个数位。 对于非数字类型,该字段表示最大字段大小 —— 换句话说就是要使用多少个来自字段内容的字符。 对于整数值则不允许使用 precision。

最后,type 确定了数据应如何呈现。

可用的字符串表示类型是:

类型

含义

's'

字符串格式。这是字符串的默认类型,可以省略。

None

和 's' 一样。

可用的整数表示类型是:

类型

含义

'b'

二进制格式。 输出以 2 为基数的数字。

'c'

字符。在打印之前将整数转换为相应的unicode字符。

'd'

十进制整数。 输出以 10 为基数的数字。

'o'

八进制格式。 输出以 8 为基数的数字。

'x'

十六进制格式。 输出以 16 为基数的数字,使用小写字母表示 9 以上的数码。

'X'

十六进制格式。 输出以 16 为基数的数字,使用大写字母表示 9 以上的数码。

'n'

数字。 这与 'd' 相似,不同之处在于它会使用当前区域设置来插入适当的数字分隔字符。

None

和 'd' 相同。

在上述的表示类型之外,整数还可以通过下列的浮点表示类型来格式化 (除了 'n' 和 None)。 当这样做时,会在格式化之前使用 float() 将整数转换为浮点数。

The available presentation types for float and

Decimal values are:

类型

含义

'e'

Scientific notation. For a given precision p,

formats the number in scientific notation with the

letter 'e' separating the coefficient from the exponent.

The coefficient has one digit before and p digits

after the decimal point, for a total of p + 1

significant digits. With no precision given, uses a

precision of 6 digits after the decimal point for

float, and shows all coefficient digits

for Decimal. 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.

'f'

Fixed-point notation. For a given precision p,

formats the number as a decimal number with exactly

p digits following the decimal point. With no

precision given, uses a precision of 6 digits after

the decimal point for float, and uses a

precision large enough to show all coefficient digits

for Decimal. If no digits follow the

decimal point, the decimal point is also removed unless

the # option is used.

'F'

定点表示。 与 'f' 相似,但会将 nan 转为 NAN 并将 inf 转为 INF。

'g'

常规格式。 对于给定的精度 p >= 1,这会将数值舍入到 p 位有效数字,再将结果以定点格式或科学计数法进行格式化,具体取决于其值的大小。

准确的规则如下:假设使用表示类型 'e' 和精度 p-1 进行格式化的结果具有指数值 exp。 那么如果 m <= exp < p,其中 m 以 -4 表示浮点值而以 -6 表示 Decimal 值,该数字将使用类型 'f' 和精度 p-1-exp 进行格式化。 否则的话,该数字将使用表示类型 'e' 和精度 p-1 进行格式化。 在两种情况下,都会从有效数字中移除无意义的末尾零,如果小数点之后没有余下数字则小数点也会被移除,除非使用了 '#' 选项。

正负无穷,正负零和 nan 会分别被格式化为 inf, -inf, 0, -0 和 nan,无论精度如何设定。

A precision of 0 is treated as equivalent to a

precision of 1. With no precision given, uses a

precision of 6 significant digits for

float, and shows all coefficient digits

for Decimal.

'G'

常规格式。 类似于 'g',不同之处在于当数值非常大时会切换为 'E'。 无穷与 NaN 也会表示为大写形式。

'n'

数字。 这与 'g' 相似,不同之处在于它会使用当前区域设置来插入适当的数字分隔字符。

'%'

百分比。 将数字乘以 100 并显示为定点 ('f') 格式,后面带一个百分号。

None

类似于 'g',不同之处在于当使用定点表示法时,小数点后将至少显示一位。 默认精度与表示给定值所需的精度一样。 整体效果为与其他格式修饰符所调整的 str() 输出保持一致。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值