Is there any difference between print 3 and print '3' in Python ?

R

redstone-cold

I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ?
 
C

Chris Angelico

I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ?

One of them takes the integer 3, converts it into a string, and prints
it. The other takes the string '3' and prints it. There's a lot of
difference under the covers, but both will print a 3, followed by a
newline.

ChrisA
 
R

Robert Kern

I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ?

Yes, there is a difference, but not much.

[~]
|6> import dis

[~]
|7> dis.disassemble(compile('print 3', '<string>', 'exec'))
1 0 LOAD_CONST 0 (3)
3 PRINT_ITEM
4 PRINT_NEWLINE
5 LOAD_CONST 1 (None)
8 RETURN_VALUE

[~]
|8> dis.disassemble(compile('print "3"', '<string>', 'exec'))
1 0 LOAD_CONST 0 ('3')
3 PRINT_ITEM
4 PRINT_NEWLINE
5 LOAD_CONST 1 (None)
8 RETURN_VALUE


As you can see, the only difference is in the first instruction. Both of these
put the object that you specified by the literal onto the stack. The difference
is that one is the int object specified by the literal 3 and the other is the
str object specified by the literal "3". Both of these objects happen to give
the same __str__ output, so that's what gets printed.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
D

Dave Angel

I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ?

This is a non-question. The input is the same, the output is the same,
what else matters?

On the other hand, if you want to dig deeper, there are lots of differences:

1) the former has a shorter source file
2) different C code is utilized inside the interpreter
3) different machine code executes
4) the temporary objects created have different id's and types
5) different execution times (by a trivial amount)
6) it takes different keystrokes to edit the two source files once you
want to make it do something useful
7) the processor works a little harder on one than the other, possibly
resulting in a different power consumption
8) different byte code is produced

Or you could be asking about Python version 3, in which case
1) the syntax error message points to a different character
 
R

redstone-cold

在 2012å¹´3月26日星期一UTC+8下åˆ8æ—¶11分03秒,Dave Angel写é“:
This is a non-question. The input is the same, the output is the same,
what else matters?

On the other hand, if you want to dig deeper, there are lots of differences:

1) the former has a shorter source file
2) different C code is utilized inside the interpreter
3) different machine code executes
4) the temporary objects created have different id's and types
5) different execution times (by a trivial amount)
6) it takes different keystrokes to edit the two source files once you
want to make it do something useful
7) the processor works a little harder on one than the other, possibly
resulting in a different power consumption
8) different byte code is produced

Or you could be asking about Python version 3, in which case
1) the syntax error message points to a different character



在 2012å¹´3月26日星期一UTC+8下åˆ8æ—¶11分03秒,Dave Angel写é“:
This is a non-question. The input is the same, the output is the same,
what else matters?

On the other hand, if you want to dig deeper, there are lots of differences:

1) the former has a shorter source file
2) different C code is utilized inside the interpreter
3) different machine code executes
4) the temporary objects created have different id's and types
5) different execution times (by a trivial amount)
6) it takes different keystrokes to edit the two source files once you
want to make it do something useful
7) the processor works a little harder on one than the other, possibly
resulting in a different power consumption
8) different byte code is produced

Or you could be asking about Python version 3, in which case
1) the syntax error message points to a different character

Oh ,God ! I think this is what I really want to know ,thank you very much !
 
R

redstone-cold

在 2012å¹´3月26日星期一UTC+8下åˆ8æ—¶11分03秒,Dave Angel写é“:
This is a non-question. The input is the same, the output is the same,
what else matters?

On the other hand, if you want to dig deeper, there are lots of differences:

1) the former has a shorter source file
2) different C code is utilized inside the interpreter
3) different machine code executes
4) the temporary objects created have different id's and types
5) different execution times (by a trivial amount)
6) it takes different keystrokes to edit the two source files once you
want to make it do something useful
7) the processor works a little harder on one than the other, possibly
resulting in a different power consumption
8) different byte code is produced

Or you could be asking about Python version 3, in which case
1) the syntax error message points to a different character



在 2012å¹´3月26日星期一UTC+8下åˆ8æ—¶11分03秒,Dave Angel写é“:
This is a non-question. The input is the same, the output is the same,
what else matters?

On the other hand, if you want to dig deeper, there are lots of differences:

1) the former has a shorter source file
2) different C code is utilized inside the interpreter
3) different machine code executes
4) the temporary objects created have different id's and types
5) different execution times (by a trivial amount)
6) it takes different keystrokes to edit the two source files once you
want to make it do something useful
7) the processor works a little harder on one than the other, possibly
resulting in a different power consumption
8) different byte code is produced

Or you could be asking about Python version 3, in which case
1) the syntax error message points to a different character

Oh ,God ! I think this is what I really want to know ,thank you very much !
 
S

Stefan Behnel

(e-mail address removed), 26.03.2012 16:28:
在 2012å¹´3月26日星期一UTC+8下åˆ8æ—¶11分03秒,Dave Angel写é“:

Oh ,God !

I don't think she takes any responsibility for the list above.

Stefan
 
T

Terry Reedy

I know the print statement produces the same result when both of
these two instructions are executed ,I just want to know Is there any
difference between print 3 and print '3' in Python ?

If you want to see the difference between the number and string
representation thereof, use repr(x).
print(repr(3), repr('3'), [3, '3'])
3 '3' [3, '3']

Note that printing a collection prints the repr() of each item precisely
so one can tell the difference between the item being a number or a string.
 
J

J. Cliff Dyer

As others have pointed out, the output is the same, because the result
of converting an integer to a string is the string of that integer.
However, other numeric literals might not do what you want, due to the
fact that they are converted to an internal numeric representation, then
converted back to a string in a canonical format.
3.0 3e0

You might think that the take away message is to use the string
representation, since it prints what you tell it to print. However, if
you use a number, you can specify the output formatting with more
fine-grained control, and even exert that control on calculated number:
3.00

This is better because you can't perform math on a string:
2.001.00
print '2 + 1'
2 + 1

So in general, you should use numbers, and then format them using
standard string formatting operations when you want to print them.
There's more information on how to do formatting here:
http://docs.python.org/library/stdtypes.html#string-formatting


Cheers,
Cliff
 
P

Peter Otten

I know the print statement produces the same result when both of these two
instructions are executed ,I just want to know Is there any difference
between print 3 and print '3' in Python ?

The question you really wanted to ask is: under what circumstances will the
two statements produce different output? Here's what I found:

$ PYTHONIOENCODING=hex python -c'from __future__ import unicode_literals
print 3'
3
$ PYTHONIOENCODING=hex python -c'from __future__ import unicode_literals
print "3"'
33
$

:)
 
S

Steven D'Aprano

This is a non-question. The input is the same, the output is the same,
what else matters?

def fib1(n):
if n == 0: return 0
elif n == 1: return 1
f2, f1 = 0, 1
for _ in range(2, n+1):
f2, f1 = f1, f2 + f1
return f1

def fib2(n):
if n == 0: return 0
elif n == 1: return 1
else: return fib2(n-1) + fib2(n-2)


Try calling fib1(35) and fib2(35). Still think only the input and output
matter? :)


For the record, fib2(35) ends up making a total of 29860703 function
calls, compared to 35 iterations for fib1.
 
R

rusi

def fib1(n):
    if n == 0: return 0
    elif n == 1: return 1
    f2, f1 = 0, 1
    for _ in range(2, n+1):
        f2, f1 = f1, f2 + f1
    return f1

def fib2(n):
    if n == 0: return 0
    elif n == 1: return 1
    else: return fib2(n-1) + fib2(n-2)

Try calling fib1(35) and fib2(35). Still think only the input and output
matter? :)

For the record, fib2(35) ends up making a total of 29860703 function
calls, compared to 35 iterations for fib1.

http://www.cse.buffalo.edu/~rapaport/intensional.html
 
I

iMath

在 2012å¹´3月26日星期一UTC+8下åˆ7æ—¶45分26秒,iMath写é“:
I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ?

thx everyone
 
I

Ian Foote

在 2012å¹´3月26日星期一UTC+8下åˆ7æ—¶45分26秒,iMath写é“:
thx everyone

The difference is that 3 is an integer whereas '3' is a string. The
print statement (function in python 3) converts any object to a string
before displaying it on the screen, so print 3 and print '3' both
display the same result.

Ian F
 
B

Benjamin Kaplan

Here's a future import though I used,so I can use the planned 3 with a 2x
python version in the command line interpreter:

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\david>c:\python26\python.exe
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)]
on
win32
Type "help", "copyright", "credits" or "license" for more information.
C:\Users\david>c:\python27_64\python.exe
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)]on
win
32
Type "help", "copyright", "credits" or "license" for more information.
In other words type(value), and find out the difference.

Somewhat OT, but __future__ doesn't work like that. You have to import
the specific features you want to use.

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.File "<stdin>", line 1
print 3
^
SyntaxError: invalid syntax
 
T

Terry Reedy

On Sun, Sep 9, 2012 at 10:41 AM, Ian Foote <[email protected]

On 09/09/12 14:23, iMath wrote:

在 2012å¹´3月26日星期一UTC+8下åˆ7æ—¶45分26秒,__iMath写é“:

I know the print statement produces the same result when
both of these two instructions are executed ,I just want to
know Is there any difference between print 3 and print '3'
in Python ?

thx everyone


Here's a future import though I used,so I can use the planned 3 with a
2x python version in the command line interpreter:

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\david>c:\python26\python.exe
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit
(Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
C:\Users\david>c:\python27_64\python.exe
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit
(AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
In other words type(value), and find out the difference.

print(x) prints str(x), which is meant to be a 'friendly'
representation. To see a difference,
'3'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top