简体   繁体   English

在python中使用print()的重要性

[英]significance of using print() in python

What is the difference between using print() and not using it. 使用print()和不使用它有什么区别。

For example, say a = ("first", "second", "third') , what is the difference between 例如,假设a = ("first", "second", "third') ,它们之间有什么区别

print (a[0] a[2])

and

a[0] a[2]

?

>>> s = 'foo'
>>> s
'foo'
>>> print s
foo

When you type any expression into the Python interpreter, if said expression returns a value, the interpreter will output that value's representation, or repr . 当您在Python解释器中键入任何表达式时,如果所述表达式返回一个值,则解释器将输出该值的表示或repr repr s are primarily used for debugging, and are intended to show the value in a way that is useful for the programmer. repr主要用于调试,旨在以对程序员有用的方式显示值。 A typical example of a value's repr is how repr('foo') would output 'foo' . 值的repr典型示例是repr('foo')将如何输出'foo'

When you use print , you aren't returning a value and so the interpreter is not actually outputting anything; 当你使用print ,你没有返回一个值,所以解释器实际上并没有输出任何东西; instead, print is writing the value's str to sys.stdout (or an alternative stream, if you specify it with the >> syntax, eg print >>sys.stderr, x ). 相反, print正在将值的str写入sys.stdout (或替代流,如果使用>>语法指定它,例如print >>sys.stderr, x )。 str s are intended for general output, not just programmer use, though they may be the same as repr . str s用于一般输出,不仅仅是程序员使用,尽管它们可能与repr相同。 A typical example of a value's str is how str('foo') would output foo . str典型示例是str('foo')如何输出foo

The difference between what the interpreter does and what print comes more into play when you write modules or scripts. 当您编写模块或脚本时,解释器的作用与print之间的区别更大。 print statements will continue to produce output, while expression values are not output unless you do so explicitly. print语句将继续生成输出,而表达式值不会输出,除非您明确这样做。 You can still output a value's repr , though: print repr(value) 您仍然可以输出值的repr ,但是: print repr(value)

You can also control str and repr in your own objects: 您还可以在自己的对象中控制strrepr

>>> class MyThing(object):
...     def __init__(self, value):
...         self.value = value
...     def __str__(self):
...         return str(self.value)
...     def __repr__(self):
...         return '<MyThing value=' + repr(self.value) + '>'
...
>>> mything = MyThing('foo')
>>> mything
<MyThing value='foo'>
>>> print mything
foo

In interactive mode, the difference is negligible, as the other answers indicate. 在交互模式中,差异可以忽略不计,正如其他答案所示。

However, in a script, print a[0] will actually print output to the screen, while just a[0] will return the value, but that has no visible effect. 但是,在脚本中, print a[0]实际上会将输出打印到屏幕上,而只有a[0]返回该值,但是没有明显的效果。

For example, consider the following script, printtest.py : 例如,请考虑以下脚本printtest.py

myList = ["first", "second", "third"]

print "with print:", myList[0], myList[2]

"without print:", myList[0], myList[2]

If you run this script in a terminal ( python printtest.py ), the output is: 如果在终端( python printtest.py )中运行此脚本,则输出为:

with print: first third 印刷品:第三名

>>> a=("first","second")
>>> print a[0],a[1]
first second
>>> a[0],a[1]
('first', 'second')

print() and not using it? print()而不使用它?

print prints value ( What I mean is in following example, read comets I added ): print打印值( 我的意思是在下面的例子中,我添加的读取彗星 ):

>>> print a[1]
second           # prints without '
>>> a[1]
'second'         # prints with ' 

more useful: 更有用:

print: 打印:

>>> print "a\nb"  
a                # print value   
b

but interpreter 但翻译

>>> "a\na"       # raw strings 
'a\na'

that is raw: 这是原始的:

>>> print repr("a\na")
'a\na'

difference: print (a[0] a[2]) and a[0] a[2]? 差异: 打印(a [0] a [2])和[0] a [2]?

This print two elements of a tuple. 这种打印的两个元素a元组。 as below 如下

>>> print a[0], a[2]
first third  

this is similar to print two strings like below: 这类似于打印两个字符串,如下所示:

>>> print "one", "two"
one two

[second] [第二]

Where as this first create a tuple (a[0], a[2]) then that will be printed 这首先创建一个元组(a[0], a[2])然后将被打印

>>> print (a[0], a[2])
('first', 'third')

first make a tuple of 2 strings then print that like below: 首先制作2个字符串的元组然后打印如下:

>>> print ("one", "two")
('one', 'two')  

Additionally, if you add , then it makes a tuple: 另外,如果你添加,那么它会产生一个元组:

simple string 简单的字串

>>> a[0]
'first'

and this is tuple: 这是元组:

>>> a[0],
('first',) 

similarly, 同样,

>>> a[0], a[1]
('first', 'second')  

you can do this 你可以这样做

>>> print (a[0], a[2], a[3])
('first', 'second', 'third')

try it :) 试试吧 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM