2

What is the difference between using print() and not using it.

For example, say a = ("first", "second", "third'), what is the difference between

print (a[0] a[2])

and

a[0] a[2]

?

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208

5 Answers5

6
>>> 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. reprs are primarily used for debugging, and are intended to show the value in a way that is useful for the programmer. A typical example of a value's repr is how repr('foo') would output 'foo'.

When you use print, you aren't returning a value and so the interpreter is not actually outputting anything; instead, print is writing the value's str to sys.stdout (or an alternative stream, if you specify it with the >> syntax, e.g. print >>sys.stderr, x). strs are intended for general output, not just programmer use, though they may be the same as repr. A typical example of a value's str is how str('foo') would output foo.

The difference between what the interpreter does and what print comes more into play when you write modules or scripts. print statements will continue to produce output, while expression values are not output unless you do so explicitly. You can still output a value's repr, though: print repr(value)

You can also control str and repr in your own objects:

>>> 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
Mattie
  • 20,280
  • 7
  • 36
  • 54
4

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.

For example, consider the following script, 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:

with print: first third

Junuxx
  • 14,011
  • 5
  • 41
  • 71
1
>>> a=("first","second")
>>> print a[0],a[1]
first second
>>> a[0],a[1]
('first', 'second')
Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64
1

print() and not using it?

print prints value (What I mean is in following example, read comets I added):

>>> 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]?

This print two elements of a tuple. 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

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

first make a tuple of 2 strings then print that like below:

>>> 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')  
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
1

you can do this

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

try it :)

Luis Pedro
  • 21
  • 2