小码哥的IT人生

Python 字符串 format() 方法

Python基础 2022-06-06 15:20:27小码哥的IT人生shichen

Python 字符串 format() 方法

实例

将价格插入占位符内,价格应为定点,两位十进制格式:

txt = "For only {price:.2f} dollars!"
print(txt.format(price = 49))

完整实例:

txt = "For only {price:.2f} dollars!"
print(txt.format(price = 49))

定义和用法

format() 方法格式化指定的值,并将其插入字符串的占位符内。

占位符使用大括号 {} 定义。请在下面的“占位符”部分中了解有关占位符的更多信息。

format() 方法返回格式化的字符串。

语法

string.format(value1, value2...)

参数值

参数 描述
value1, value2...

必需。一个或多个应该格式化并插入字符串的值。值可以是数字,用于指定要删除的元素的位置。

这些值可以是用逗号分隔的值列表、键=值列表,或两者的组合。

这些值可以是任何数据类型。

占位符

可以使用命名索引 {price}、编号索引{0}、甚至空的占位符 {} 来标识占位符。

示例代码:

使用不同的占位符值:

txt1 = "My name is {fname}, I'am {age}".format(fname = "Bill", age = 64)
txt2 = "My name is {0}, I'am {1}".format("Bill",64)
txt3 = "My name is {}, I'am {}".format("Bill",64)

完整实例:

#named indexes:
txt1 = "My name is {fname}, I'm {age}".format(fname = "Bill", age = 63)
#numbered indexes:
txt2 = "My name is {0}, I'm {1}".format("Bill",63)
#empty placeholders:
txt3 = "My name is {}, I'm {}".format("Bill",63)
print(txt1)
print(txt2)
print(txt3)

格式化类型

在占位符内,您可以添加格式化类型以格式化结果:

:< 试一试 左对齐结果(在可用空间内)
:> 试一试 右对齐结果(在可用空间内)
:^ 试一试 居中对齐结果(在可用空间内)
:= 试一试 将标志放置在最左侧
:+ 试一试 使用加号指示结果是正数还是负数
:- 试一试 负号仅用于负值
: 试一试 使用空格在正数之前插入一个多余的空格(在负数之前使用减号)
:, 试一试 使用逗号作为千位分隔符
:_ 试一试 使用下划线作为千位分隔符
:b 试一试 二进制格式
:c   将值转换为相应的 unicode 字符
:d 试一试 十进制格式
:e 试一试 科学格式,带有小写字母 E
:E 试一试 科学格式,带有大写字母 E
:f 试一试 定点数字格式
:F 试一试 定点数字格式,以大写形式显示(将 inf 和 nan 显示为 INF 和 NAN)
:g   通用格式
:G   通用格式(将大写 E 用作科学计数法)
:o 试一试 八进制格式
:x 试一试 十六进制格式,小写
:X 试一试 十六进制格式,大写
:n   数字格式
:% 试一试 百分比格式

完整实例1:

#To demonstrate, we insert the number 8 to set the available space for the value to 8 characters.
#Use ">" to right-align the value:
txt = "We have {:>8} chickens."
print(txt.format(49))

完整实例2:

#To demonstrate, we insert the number 8 to set the available space for the value to 8 characters.
#Use "^" to center-align the value:
txt = "We have {:^8} chickens."
print(txt.format(49))

完整实例3:

#To demonstrate, we insert the number 8 to specify the available space for the value.
#Use "=" to place the plus/minus sign at the left most position:
txt = "The temperature is {:=8} degrees celsius."
print(txt.format(-5))

完整实例4:

#Use "+" to always indicate if the number is positive or negative:
txt = "The temperature is between {:+} and {:+} degrees celsius."
print(txt.format(-3, 7))

完整实例5:

#Use "-" to always indicate if the number is negative (positive numbers are displayed without any sign):
txt = "The temperature is between {:-} and {:-} degrees celsius."
print(txt.format(-3, 7))

完整实例6:

#Use " " (a space) to insert a space before positive numbers and a minus sign before negative numbers:
txt = "The temperature is between {: } and {: } degrees celsius."
print(txt.format(-3, 7))

完整实例7:

#Use "," to add a comma as a thousand separator:
txt = "The universe is {:,} years old."
print(txt.format(13800000000))

完整实例8:

#Use "_" to add a underscore character as a thousand separator:
txt = "The universe is {:_} years old."
print(txt.format(13800000000))

完整实例9:

#Use "b" to convert the number into binary format:
txt = "The binary version of {0} is {0:b}"
print(txt.format(5))

完整实例10:

#Use "d" to convert a number, in this case a binary number, into decimal number format:
txt = "We have {:d} chickens."
print(txt.format(0b101))

完整实例11:

#Use "e" to convert a number into scientific number format (with a lower-case e):
txt = "We have {:e} chickens."
print(txt.format(5))

完整实例12:

#Use "E" to convert a number into scientific number format (with an upper-case E):
txt = "We have {:E} chickens."
print(txt.format(5))

完整实例13:

#Use "f" to convert a number into a fixed point number, default with 6 decimals, but use a period followed by a number to specify the number of decimals:
txt = "The price is {:.2f} dollars."
print(txt.format(45))
#without the ".2" inside the placeholder, this number will be displayed like this:
txt = "The price is {:f} dollars."
print(txt.format(45))

完整实例14:

#Use "F" to convert a number into a fixed point number, but display inf and nan as INF and NAN:
x = float('inf')
txt = "The price is {:F} dollars."
print(txt.format(x))
#same example, but with a lower case f:
txt = "The price is {:f} dollars."
print(txt.format(x))

完整实例15:

#Use "o" to convert the number into octal format:
txt = "The octal version of {0} is {0:o}"
print(txt.format(10))

完整实例16:

#Use "x" to convert the number into Hex format:
txt = "The Hexadecimal version of {0} is {0:x}"
print(txt.format(255))

完整实例17:

#Use "X" to convert the number into upper-case Hex format:
txt = "The Hexadecimal version of {0} is {0:X}"
print(txt.format(255))

完整实例18:

#Use "%" to convert the number into a percentage format:
txt = "You scored {:%}"
print(txt.format(0.25))
#Or, without any decimals:
txt = "You scored {:.0%}"
print(txt.format(0.25))

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024