这个 Python 字符串函数根据我们的要求格式化输出。这个函数是 print 函数中格式化字符串的方法之一。
在本节中,我们将通过一个示例向您解释如何在 Python 编程中使用字符串格式函数,其语法如下所示。
{}.format(value)
以下示例集有助于理解字符串格式函数。
Python 格式字符串 {} 示例
format 字符串方法使用花括号 {} 作为替换占位符。这意味着一个 {} 花括号接受一个参数。
我们在第三行声明了一个字符串变量,而不是直接在 print 语句中提供字符串值。接下来,我们调用了 msg。
print('{}'.format(25))
msg = 'Tutorial Gateway'
print('{}'.format(msg)
25
Tutorial Gateway
如何使用 {} 格式作为字符串字面量?
此方法使用 {} 作为替换字段的占位符。 {} 之外的任何内容都被视为 Python 字符串字面量,会原样打印。
在此 Python 格式字符串示例中,第一个语句用 Hello 替换 {},并原样打印 World。
第二个格式字符串语句在“Employee’s Age is 25”之后替换了 {}。
print("{} World".format("Hello"))
print("Employee Age is {}".format(25))
Hello World
Employee Age is 25
如何在 Python 中格式化多个字符串参数?
我们在该函数中使用了多个参数。在这种情况下,print 格式函数使用我们在 format 和字符串中指定的顺序。
例如,在第一个语句中,第一个花括号用 Tutorial 填充或替换,第二个花括号用 Gateway 替换。它始终遵循相同的顺序。
print('{} {}'.format('Tutorial', 'Gateway'))
print('{} {}'.format('Tutorial', 25))
Tutorial Gateway
Tutorial 25
如果我们声明两个变量,我们可以用这些字符串变量替换此函数中的文本。
Python 格式字符串索引示例
到目前为止,我们已经使用了 {}. That’s why those curly braces are replaced by the position of arguments in this method. In this example, we used the index values to change the position.
第二个语句在 {1} 处打印第二个参数,在 {0} 处打印第一个参数值,在 {2} 处打印第三个参数值占位符。

使用关键字参数的 format 函数
在此编程语言中,您可以在此字符串格式函数中使用关键字参数,如 key = value。
在此 字符串函数 示例中,第一个语句中,key1 是 lang,value1 = Py。接下来,key2 = com,value2 = Tutorial Gateway。这意味着 key 被 value1 替换,key2 被 value2 替换。
f_msg = 'Tutorial Gateway'
s_msg = 'Py'
print('Learn {lang} at {com}'.format(lang = s_msg, com = f_msg))
print('{com} Welcomes {lang}'.format(lang = s_msg, com = f_msg))
Learn Py at Tutorial Gateway
Tutorial Gateway Welcomes Py
如何使用 Python 格式函数截断字符串?
您还可以使用此格式函数来截断字符串。此示例说明如何使用格式函数来截断字符串值。
第一个语句将字符串截断到长度为 5。下一个语句截断到长度为 7,如果我们放置 10:,它将返回字符串的前 10 个字符,即 Tutorial G。
msg = 'Tutorial Gateway'
print('{:.5}'.format(msg))
print('{:.7}'.format(msg))
Tutor
Tutoria
Python 格式字符串选项
以下是可用于对齐、符号、二进制等的格式选项列表。
- ^ 将结果对齐到中心文本。
- < 将结果对齐到左侧。或左对齐文本。
- > 将结果对齐到右侧。或右对齐文本。
- = 将符号放在最左侧位置。
- + 使用此符号指示结果是正数还是负数。
- – 仅对负值使用此减号。
- ‘ ‘ – 对于正数,它使用前导空格字符。
- , – 它使用逗号作为千位分隔符。
- _ 它使用下划线作为千位分隔符。
- b – 二进制。
- c – 将给定值转换为 Unicode 字符。
- d – 十进制。
- e – 科学计数法(小写)。
- E – 科学计数法(大写)。
- f – 定点数。
- F – 大写定点数。
- g – 通用。
- G – 通用。对于科学计数法,它使用 E)。
- o – 八进制整数。
- x – 十六进制小写字母。
- X – 十六进制大写字母。
- n – 数字。
- % – 百分比。
Python 字符串格式填充
如前所述,<, > 和 ^ 用于将给定的数字或文本对齐到左侧、右侧或中心。在此示例中,我们将这些运算符与此函数结合使用以执行对齐。

格式字符串填充分析
- print(‘{:^40}’) 返回宽度为 40 的消息。这里,我们在 40 之前使用了 ^。这意味着消息在中心对齐,总宽度为 40(包括空格和文本)。
- print(‘{:*^40}’) 与上面的语句相同。我们使用了 * 符号而不是显示空空格。这意味着所有空空格都填充了 * 符号。
- print(‘{:@^{}s}’) – 您也可以使用花括号作为宽度值,并将这些值分配给格式字符串函数参数。这里,我们将 msg 右对齐,宽度为 40,宽度用 @ 符号填充。
- print(‘{:25}’) 或 print(‘{:<25}’) 返回宽度为 25 的消息。这里,< 将文本左对齐,并将剩余的宽度用空空格填充。
- print(‘{:>30}’) 返回宽度为 30 的 msg。这里,> 符号将文本右对齐,并将剩余的宽度用空空格填充。
如何在 Python 中格式化数字?
我们对数字使用了 print 格式方法。第一个语句在 95 前面放置一个空格,第二个语句在 95 前面返回一个正号。类似地,如果我们用 – 替换 +,则为负数返回负号。
print('{: d}'.format(95))
print('{:+d}'.format(95))
95
+95
Python 格式或格式化分隔符
使用千位分隔符或更改分隔符。此语句使用逗号作为千位分隔符。如果我们用 {:_} 替换 {:,},它会返回 _ 下划线作为千位分隔符,即 100_000。
value = 100000
print('{:,}'.format(value))
100,000
您可以使用此来为值应用填充。首先,我们声明了正整数和负整数。接下来,我们使用了整数值进行填充。
value = 100
value2 = -200
print('{:10d}'.format(value))
print('{:<10d}'.format(value))
print('{:^10d}'.format(value))
print('{:=10d}'.format(value2))
100
100
100
- 200
填充分析
- {:10d} 表示它取 100 并将宽度指定为 10。这里,我们没有指定对齐 <, > 或 ^,所以它在右侧(默认)以 10 的宽度对齐。
- {:<10d} 表示 100 左对齐,宽度 = 10。
- {:^10d} 将 100 对齐到中心,宽度为 10。
- {:=10d} 将负号保留在左侧,将 200 保留到右侧。这里,总宽度为 10。
格式化浮点数或浮点值
您可以在浮点值上使用字符串格式内置函数。我的意思是,您可以限制小数位数,或者增加小数位数。
例如,.2f 表示只有 2 位小数。如果我们指定 .4f ({:4f},它会返回带有 4 位小数的值,并将剩余值四舍五入为 10.9824。
value = 10.9823764989
print('{:.2f}'.format(value))
10.98
Python 格式整数和十进制示例
使用此字符串函数样式化数值和浮点数。
value = 100
f_value = 15.957639
print('{:5d}'.format(value))
print('{:07d}'.format(value))
print('{:6.2f}'.format(f_value))
print('{:012.4f}'.format(f_value))
100
0000100
15.96
0000015.9576
在此格式打印示例中,
- {:5d} 表示它返回 100,宽度为 5。如果您指定任何对齐,则 100 会对齐到该侧。否则,默认情况下,它会右对齐
- {:07d} 表示 100 返回的宽度为 7。这里,所有空空格都因 07d 而被 0 填充。
- {:6.2f} 返回 f_value,小数点后保留两位 (.2f)。接下来,我们指定宽度为 6。因此,它返回宽度为 6 的 f_value。
- {:012.4f} 取 15.957639 并返回四位小数。这里,宽度值为 12;我们在该宽度值前使用了 0。因此,15.9576 返回宽度为 12,剩余的空空格用 0 填充。
格式化二进制值
在本节中,我们将使用所有现有的样式选项。首先,我们声明了一个值为 10000。这里,我们使用该值执行二进制、八进制、十六进制和数字函数。
接下来,我们使用不同的值作为格式字符串参数来返回科学计数、通用、百分比和定点数。
value = 100000
{:.2f} – 100000.00
{:b} – 11000011010100000
{:o} – 303240
{:x} – 186a0
{:X} – 186A0
{:n} – 100000
{:c} of 120 = x
{:e} of 5.00023455341 = 5.000235e+00
{:E} of 5.00023455341 = 5.000235E+00
{:f} of 25.98723453422 = 25.987235
{:F} of 25.98723453422 = 25.987235
{:g} of 15.987234534 = 15.9872
{:G} of 0.987234534 = 0.987235
{:.2} of 0.987234 = 0.99
{:%} of 0.87 = 87.000000%
Python 格式字符串列表示例
使用此函数样式化 列表 项。
fruits = ['apple', 'mango', 'banana', 'cherry','kiwi']
print('Fruits List = {}'.format(fruits))
print('Fruit Item = {0[1]}'.format(fruits))
print('Fruit Item = {f[1]}'.format(f = fruits))
print('Fruit Item = {}'.format(*fruits))
Fruits List = ['apple', 'mango', 'banana', 'cherry', 'kiwi']
Fruit Item = mango
Fruit Item = mango
Fruit Item = apple
格式化字典项
对 字典 元素使用 print 函数。
employee = {'name': 'John', 'age': 25, 'salary': 120000}
print('{} is {} years Old'.format(employee['name'], employee['age']))
print('At {1}, {0} is earning {2} income'.format( employee['name'], employee['age'], employee['salary']))
print('-------------')
print('{name} is {age} Years Old'.format(**employee))
print('At {age},{name} is earning {salary} income'.format(**employee))
print('-------------')
print('{emp[name]} is {emp[age]} Years Old'.format(emp = employee))
print('At {emp[age]}, {emp[name]} is earning {emp[salary]} income'.format(emp = employee))
John is 25 years Old
At 25, John is earning 120000 income
-------------
John is 25 Years Old
At 25,John is earning 120000 income
-------------
John is 25 Years Old
At 25, John is earning 120000 income