从零开始学python | Python中的格式函数是什么,它如何工作?

Python中的格式化函数(str.format())是字符串类别的一种技术,可让您尝试进行变量替换和数据格式化。它使您可以通过点数据格式按所需间隔连接字符串的各个部分。本文将引导您了解Python中格式化程序的多种常见用法,这将有助于您的代码和程序变得用户友好。

这里是这里讨论的所有指针:

  • Single Formatter
  • Multiple Formatter
  • Formatters using Positional and Keyword Arguments
  • Type Specification
  • Spacing and Alignments using formatter
  • Organizing data

因此,让我们开始吧:)

1)Single Formatter

格式化程序通过将一对大括号“ {}”所概述的一个或多个替换字段或占位符固定到字符串中并调用str.format()技术来工作。您需要将要与字符串连接的值传递给format()方法。运行程序时,此值将在占位符{}定位的位置打印。可以将单个格式化程序定义为只有一个占位符的格式化程序。在下面的示例中,您将能够在print语句中看到该格式的实现。

除了直接在print语句中使用之外,我们还可以对变量使用format():

例子:

print("{} is a good option for beginners in python".format("Edureka"))

输出:    Edureka is a good option for beginners in python

除了直接在print语句中使用之外,我们还可以对变量使用format():

例子:

输出:    Edureka是python初学者的好选择

除了直接在print语句中使用之外,我们还可以对变量使用format():

例子:

my_string = "{} is a good option for beginners in python"
print(my_string.format("Edureka"))

输出:  Edureka is a good option for beginners in python

2)Multiple Formatter:

假设如果句子中需要其他变量替换,可以通过在需要替换的位置添加另一组花括号并将第二个值传递给format()来完成。然后,Python将用作为参数传递的值替换占位符。

例子:

my_string = "{} is a good option for beginners in {}"
print(my_string.format("Edureka","Machine Learning"))

输出:  Edureka is a good option for beginners in Machine Learning

您可以在给定变量中添加任意数量的占位符或大括号,以及与format()相同数量的输入。

例子:

my_string = "{} is an {} option for {} in {}"
print(my_string.format("Edureka","excellent","experienced","Machine Learning"))

输出:Edureka is an excellent option for experienced in Machine Learning

因此,继续使用Python中的Format函数

3)Formatters using Positional and Keyword Arguments:

当占位符为空{}时,Python解释器将按顺序通过str.format()替换值。

str.format()方法中存在的值主要是元组“元组是不可变的Python对象的序列”数据类型,元组中包含的每个单独项通常以其索引号(从零开始)进行引用。 。然后将这些索引号传递到原始字符串内的大括号中。

您可以使用大括号内的位置参数或索引号,以便将特定值从format()获取到变量中:

例子:

my_string = "{0} is a good option for beginners in {1}"
print(my_string.format("Edureka","Machine Learning"))

输出:  Edureka is a good option for beginners in Machine Learning

关键字参数可通过在大括号内调用该变量名称来帮助调用format()中的变量:

例子:

my_string = "{0} is a good option for beginners in {domain}"
print(my_string.format("Edureka",domain = "Machine Learning"))

输出:  Edureka is a good option for beginners in Machine Learning

我们可以同时使用关键字和位置参数:

例子:

my_string = "{domain} is a good option for beginners in {0}"
print(my_string.format("Edureka",domain = "Artificial Intelligence"))

输出:

Artificial Intelligence is a good option for beginners in Edureka

4)Type Specification:

通过使用格式代码语法,更多参数包含在语法的大括号中。在这种语法中,无论field_name在哪里,它都为str.format()技术指定参数或关键字的指示,而转换是指数据类型的转换代码。一些转换类型是:

s –字符串

d –十进制整数(以10为底)

f –浮动

c –字符

b –二进制

o –八进制

x – 9后面的小写字母的十六进制

e –指数表示法

例子:

my_string = "The Temperature in {0} today is {1:d} degrees outside!"
print(my_string.format("Vizag",22))

输出:

The Temperature in Vizag today is 22 degrees outside!

确保您使用的是正确的转换。如果使用不同的转换代码,则会出现以下错误:

例子:

my_string = "The Temperature in {0} today is {1:d} degrees outside!"
print(my_string.format("Vizag",22.025))

输出:

Traceback (most recent call last):
  File "c:/MasterData/ProjectDataPro/VSCodePro/hello_world.py", line 2, in <module>
    print(my_string.format("Vizag",22.025))
ValueError: Unknown format code 'd' for object of type 'float'

例子:

my_string = "The Temperature in {0} today is {1:.2f} degrees outside!"
print(my_string.format("Vizag",22.025))

输出:

The Temperature in Vizag today is 22.02 degrees outside!

5)Spacing and Alignments using formatter

我们可以使用format()在占位符的右侧或左侧或两侧应用空格或对齐方式。对齐代码为:

<:左对齐文字

^:中心文字

>:右对齐

例子:

my_string = "The Temperature in {0:20} today is {1:d} degrees outside!"
print(my_string.format("Vizag",22))

输出:

The Temperature in Vizag                today is 22 degrees outside!

例子:

my_string = "The Temperature in {0} today is {1:20} degrees outside!"
print(my_string.format("Vizag",22))

输出:

The Temperature in Vizag today is                   22 degrees outside!

我们可以看到字符串是左对齐的,数字是右对齐的。通过使用format(),我们可以在下面更改它们两者:

例子:

my_string = "The Temperature in {0:>20} today is {1:d} degrees outside!"
print(my_string.format("Vizag",22))

输出:

The Temperature in                Vizag today is 22 degrees outside!

6)Organizing data:

我们倾向于在Excel工作表中组织数据,在其中我们可以通过各种方法来调整列的大小,但是如何在程序中应用相同的方法,即一列中的值以指数方式递增,而一列中的项进入了其他或最终用户可能会难以理解哪个值属于哪个列。

例子:

for i in range(4,15):
    print(i,i*i,i*i*i)

输出:

4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
11 121 1331
12 144 1728
13 169 2197
14 196 2744

在这里,我们可以使用format()定义每列之间的空间,以便最终用户可以轻松地区分不同列的值。

例子:

for i in range(4,15):
    print("{:6d} {:6d} {:6d}".format(i,i*i,i*i*i))

输出:

     4     16     64
     5     25    125
     6     36    216
     7     49    343
     8     64    512
     9     81    729
    10    100   1000
    11    121   1331
    12    144   1728
    13    169   2197
    14    196   2744

从上面的用法中,我们可以说用于变量替换的格式化程序是连接字符串,转换值,组织值和数据的有效方法。格式化程序代表一种用于将变量替换传递到字符串中的简单但非描述性的方式,并且有助于创建可解密且用户友好的某些输出。

这使我们到了本文“ Python中的格式化函数”的结尾。我希望您对与您分享的所有内容都感到满意。 确保尽可能多地练习并恢复经验。  

合智互联客户成功服务热线:400-1565-661

admin
admin管理员

上一篇:【高并发系列】Java中线程到底是按什么顺序执行的?你了解的可能是错误的!
下一篇:华为云数据库GaussDB(for Cassandra)揭秘第一期: 初识GaussDB(for Cassandra)

留言评论

暂无留言
取消
扫码支持