【问题标题】:significance of using print() in python在 python 中使用 print() 的意义
【发布时间】:2013-01-23 12:20:49
【问题描述】:

使用print()和不使用有什么区别。

比如说a = ("first", "second", "third'),有什么区别

print (a[0] a[2])

a[0] a[2]

?

【问题讨论】:

  • a=("first", "second","third'), 语法错误更正它“第三”
  • print (a[0] a[2]) 一个你应该写的错误print (a[0], a[2])
  • 这不是重复的,问题是关于使用 print 而不是使用 parenthesis with print

标签: python


【解决方案1】:
>>> s = 'foo'
>>> s
'foo'
>>> print s
foo

当您在 Python 解释器中键入任何表达式时,如果该表达式返回一个值,则解释器将输出该值的表示形式,即reprreprs 主要用于调试,旨在以对程序员有用的方式显示值。值的repr 的一个典型示例是repr('foo') 如何输出'foo'

当您使用print 时,您不会返回值,因此解释器实际上不会输出任何内容;相反,print 将值的str 写入sys.stdout(或替代流,如果您使用>> 语法指定它,例如print >>sys.stderr, x)。 strs 用于一般输出,而不仅仅是程序员使用,尽管它们可能与 repr 相同。一个值的str 的典型示例是str('foo') 将如何输出foo

当您编写模块或脚本时,解释器所做的与print 之间的区别会更加明显。 print 语句将继续产生输出,而表达式值不会输出,除非您明确这样做。你仍然可以输出一个值的repr,但是:print repr(value)

您还可以在自己的对象中控制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

【讨论】:

    【解决方案2】:

    在交互模式下,差异可以忽略不计,正如其他答案所示。

    但是,在脚本中,print a[0] 将实际打印输出到屏幕,而 a[0]返回该值,但这没有可见的效果。

    例如,考虑以下脚本,printtest.py

    myList = ["first", "second", "third"]
    
    print "with print:", myList[0], myList[2]
    
    "without print:", myList[0], myList[2]
    

    如果您在终端 (python printtest.py) 中运行此脚本,则输出为:

    带打印:前三分之一

    【讨论】:

      【解决方案3】:
      >>> a=("first","second")
      >>> print a[0],a[1]
      first second
      >>> a[0],a[1]
      ('first', 'second')
      

      【讨论】:

        【解决方案4】:

        你可以这样做

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

        试试看:)

        【讨论】:

        • 不,你不能,它会给出一个 IndexError。此外,这并不能回答问题。
        【解决方案5】:

        print() 而不使用它?

        print 打印值(我的意思是在下面的例子中,阅读我添加的彗星):

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

        更有用:

        打印:

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

        但解释器

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

        那是原始的:

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

        区别:print(a[0] a[2])和a[0] a[2]?

        这会打印a 元组的两个元素。如下

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

        这类似于打印两个字符串,如下所示:

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

        [秒]

        首先创建一个元组(a[0], a[2]) 然后将其打印出来

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

        首先创建一个包含 2 个字符串的元组,然后打印如下:

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

        另外,如果你添加,,那么它会生成一个元组:

        简单的字符串

        >>> a[0]
        'first'
        

        这是元组:

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

        同样,

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-12-13
          • 1970-01-01
          • 2018-02-12
          • 2014-10-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多