Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中打印的差异_Python_Printing - Fatal编程技术网

Python中打印的差异

Python中打印的差异,python,printing,Python,Printing,我正在学习Python,下面是一些代码的示例: 您何时使用此选项: Y = "Apple" print "The object is an %s" % Y 你什么时候用这个 X = "Grape" print "The object is an " , X 为什么呢?关于何时使用第一种方法(百分比格式)的更好例子是 Y = 'Apple' print "The %s tastes sweet." % Y 它允许您轻松地将变量插入字符串,而无需执行以下操作: Y = 'Apple' prin

我正在学习Python,下面是一些代码的示例:

您何时使用此选项:

Y = "Apple"
print "The object is an %s" % Y
你什么时候用这个

X = "Grape"
print "The object is an " , X

为什么呢?

关于何时使用第一种方法(百分比格式)的更好例子是

Y = 'Apple'
print "The %s tastes sweet." % Y
它允许您轻松地将变量插入字符串,而无需执行以下操作:

Y = 'Apple'
print "The", Y, " tastes sweet."

所以这确实是个人偏好,但百分比格式允许在字符串中插入变量,而无需连接。

前者打印单个格式化字符串。后者打印两件东西,一件接一件,用空格隔开。当您想要组合字符串时,请使用字符串格式,例如用于GUI元素或作为某些处理函数的参数。将多个对象发送到
print
语句(或Python 3中的
print()
函数)主要是为了进行打印调试(尽管在命令行程序中使用它没有什么错,前提是生成的代码与使用字符串格式创建的代码一样清晰).

区别不仅仅是方便和偏好。这两种方法是两码事

让我们考虑<代码>打印“对象是一个”,x/代码>第一。的行为有点不规则和不直观,这也是Python 3使用打印函数的原因之一。在Python2中,该语句采用逗号分隔的表达式,并逐个打印出来,必要时将其转换为字符串,并使用一些规则来决定是否在每个表达式之前放置一个空格(当尚未将任何字符写入标准输出时,它会在“(1)之外放置一个空格,(2)当写入标准输出的最后一个字符是除“”以外的空白字符时,或(3)当标准输出的最后一个写入操作不是打印语句时。“)

所以当你有字符串X和Y,并且打印X,Y时,它会先打印X,然后打印Y,可能中间有空格。如果你想快速打印一大堆东西,这很好用。在某种程度上,它也是组合单独字符串的简单速记。不过,它只打印您输入的表达式的字符串表示形式。除非您已经将对象转换为您想要的字符串,否则您无法控制它们的外观。这也是特定于print语句的内容

另一方面,字符串格式的定义是它自己的事情;您不需要将其与
打印一起使用。您还可以执行类似于
a=“对象是一个%s.%X
,它将按预期工作,在X中进行替换。但这不是它所能做的全部,否则它不会被称为字符串格式。相反,它允许您控制如何将内容放入字符串,尤其是数字。这使得它更普遍地有用,即使用法有点不透明,阅读关于它的文档也是一个好主意。但是,例如:

In [1]: a = 1507.2515621

In [2]: print "a is: %d" % a # print as a signed integer
a is: 1507

In [3]: print "a is: %f" % a # print as a float, decimal format
a is: 1507.251562

In [4]: print "a is: %10.2E" % a # print as a float in exponential format, with
a is:   1.51E+03

In [5]: print "a is: %x" % a # signed hexadecimal
a is: 5e3

In [6]: print "The object is an %s." % "Apple" # a string using str()
The object is an Apple.

In [7]: print "The object is an %r." % "Apple" # a string using repr()
The object is an 'Apple'.

In [19]: z = {'a': 2, 'b': 3}

In [21]: print "a is %(a)d, and b is %(b)d." % z
a is 2, and b is 3. 
In [39]: print "a is: {0}, or {0:g}, or {0:e}, and z is {1:s},\n and a in z is {1[a]}, \
   ....: but the a variable is {0:,}.".format(a,z)
a is: 1507.2515621, or 1507.25, or 1.507252e+03, and z is {'a': 2, 'b': 3},
 and a in z is 2, but the a variable is 1,507.2515621.
但是,您应该知道,%格式不再被认为是进行字符串格式设置的“正确”方法,而且在Python3中根本不是这样。相反,Python2.6和更高版本以及Python3都具有相同的功能,它不那么紧凑,但更适合Python的其余部分(%实际上是一个重载的模运算符)。例如:

In [1]: a = 1507.2515621

In [2]: print "a is: %d" % a # print as a signed integer
a is: 1507

In [3]: print "a is: %f" % a # print as a float, decimal format
a is: 1507.251562

In [4]: print "a is: %10.2E" % a # print as a float in exponential format, with
a is:   1.51E+03

In [5]: print "a is: %x" % a # signed hexadecimal
a is: 5e3

In [6]: print "The object is an %s." % "Apple" # a string using str()
The object is an Apple.

In [7]: print "The object is an %r." % "Apple" # a string using repr()
The object is an 'Apple'.

In [19]: z = {'a': 2, 'b': 3}

In [21]: print "a is %(a)d, and b is %(b)d." % z
a is 2, and b is 3. 
In [39]: print "a is: {0}, or {0:g}, or {0:e}, and z is {1:s},\n and a in z is {1[a]}, \
   ....: but the a variable is {0:,}.".format(a,z)
a is: 1507.2515621, or 1507.25, or 1.507252e+03, and z is {'a': 2, 'b': 3},
 and a in z is 2, but the a variable is 1,507.2515621.
这有很多选择,我强烈建议阅读关于它的文档。不幸的是,它有我认为是一些不幸的设计选择,并且文档相当不透明