f



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

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 ?
0
redstone
3/26/2012 11:45:26 AM
comp.lang.python 77058 articles. 6 followers. Post Follow

16 Replies
670 Views

Similar Articles

[PageSpeed] 34

On Mon, Mar 26, 2012 at 10:45 PM,  <redstone-cold@163.com> wrote:
> 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
0
rosuav (4324)
3/26/2012 12:01:40 PM
On 3/26/2012 13:45, redstone-cold@163.com wrote:
> 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 former prints a number while the latter a string.

Kiuhnm
0
Kiuhnm
3/26/2012 12:07:10 PM
On 3/26/12 12:45 PM, redstone-cold@163.com wrote:
> 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

0
robert.kern (1692)
3/26/2012 12:10:55 PM
On 03/26/2012 07:45 AM, redstone-cold@163.com wrote:
> 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

-- 

DaveA

0
d9555 (591)
3/26/2012 12:11:03 PM
=E5=9C=A8 2012=E5=B9=B43=E6=9C=8826=E6=97=A5=E6=98=9F=E6=9C=9F=E4=B8=80UTC+=
8=E4=B8=8B=E5=8D=888=E6=97=B611=E5=88=8603=E7=A7=92=EF=BC=8CDave Angel=E5=
=86=99=E9=81=93=EF=BC=9A
> On 03/26/2012 07:45 AM, redstone-cold@163.com wrote:
> > 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 ?
>=20
> This is a non-question.  The input is the same, the output is the same,=
=20
> what else matters?
>=20
> On the other hand, if you want to dig deeper, there are lots of differenc=
es:
>=20
> 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=20
> want to make it do something useful
> 7) the processor works a little harder on one than the other, possibly=20
> resulting in a different power consumption
> 8) different byte code is produced
>=20
> Or you could be asking about Python version 3, in which case
> 1) the syntax error message points to a different character
>=20
> --=20
>=20
> DaveA



=E5=9C=A8 2012=E5=B9=B43=E6=9C=8826=E6=97=A5=E6=98=9F=E6=9C=9F=E4=B8=80UTC+=
8=E4=B8=8B=E5=8D=888=E6=97=B611=E5=88=8603=E7=A7=92=EF=BC=8CDave Angel=E5=
=86=99=E9=81=93=EF=BC=9A
> On 03/26/2012 07:45 AM, redstone-cold@163.com wrote:
> > 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 ?
>=20
> This is a non-question.  The input is the same, the output is the same,=
=20
> what else matters?
>=20
> On the other hand, if you want to dig deeper, there are lots of differenc=
es:
>=20
> 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=20
> want to make it do something useful
> 7) the processor works a little harder on one than the other, possibly=20
> resulting in a different power consumption
> 8) different byte code is produced
>=20
> Or you could be asking about Python version 3, in which case
> 1) the syntax error message points to a different character
>=20
> --=20
>=20
> DaveA

Oh ,God !  I think this is what I really want to know ,thank you very much =
!
0
redstone
3/26/2012 2:28:25 PM
=E5=9C=A8 2012=E5=B9=B43=E6=9C=8826=E6=97=A5=E6=98=9F=E6=9C=9F=E4=B8=80UTC+=
8=E4=B8=8B=E5=8D=888=E6=97=B611=E5=88=8603=E7=A7=92=EF=BC=8CDave Angel=E5=
=86=99=E9=81=93=EF=BC=9A
> On 03/26/2012 07:45 AM, redstone-cold@163.com wrote:
> > 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 ?
>=20
> This is a non-question.  The input is the same, the output is the same,=
=20
> what else matters?
>=20
> On the other hand, if you want to dig deeper, there are lots of differenc=
es:
>=20
> 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=20
> want to make it do something useful
> 7) the processor works a little harder on one than the other, possibly=20
> resulting in a different power consumption
> 8) different byte code is produced
>=20
> Or you could be asking about Python version 3, in which case
> 1) the syntax error message points to a different character
>=20
> --=20
>=20
> DaveA



=E5=9C=A8 2012=E5=B9=B43=E6=9C=8826=E6=97=A5=E6=98=9F=E6=9C=9F=E4=B8=80UTC+=
8=E4=B8=8B=E5=8D=888=E6=97=B611=E5=88=8603=E7=A7=92=EF=BC=8CDave Angel=E5=
=86=99=E9=81=93=EF=BC=9A
> On 03/26/2012 07:45 AM, redstone-cold@163.com wrote:
> > 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 ?
>=20
> This is a non-question.  The input is the same, the output is the same,=
=20
> what else matters?
>=20
> On the other hand, if you want to dig deeper, there are lots of differenc=
es:
>=20
> 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=20
> want to make it do something useful
> 7) the processor works a little harder on one than the other, possibly=20
> resulting in a different power consumption
> 8) different byte code is produced
>=20
> Or you could be asking about Python version 3, in which case
> 1) the syntax error message points to a different character
>=20
> --=20
>=20
> DaveA

Oh ,God !  I think this is what I really want to know ,thank you very much =
!
0
redstone
3/26/2012 2:28:25 PM
redstone-cold@163.com, 26.03.2012 16:28:
> 在 2012年3月26日星期一UTC+8下午8时11分03秒,Dave Angel写道:
>> On 03/26/2012 07:45 AM, redstone-cold@163.com wrote:
>>> 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
> 
> Oh ,God !

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

Stefan

0
stefan_ml (868)
3/26/2012 3:03:21 PM
On 3/26/2012 7:45 AM, redstone-cold@163.com wrote:
> 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.

-- 
Terry Jan Reedy

0
tjreedy (5460)
3/26/2012 3:45:51 PM
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.

>>> print 3, '3'
3 3
>>> print 3.00, '3.00'
3.0 3.00
>>> print 0x3, '0x3'
3 0x3
>>> print 03, '03'
3 03
>>> print 3e0, '3e0'
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:

>>> print '%0.2f' % (3,)
3.00
>>> print '%0.2f' % (2 + 1)
3.00

This is better because you can't perform math on a string:

>>> print '2' + '1'
21
>>> print '2.00' + '1.00'
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


On Mon, 2012-03-26 at 04:45 -0700, redstone-cold@163.com wrote:
> 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 ?


0
jcd1 (296)
3/26/2012 5:06:58 PM
redstone-cold@163.com wrote:

> 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
$

:)

0
__peter__ (4031)
3/26/2012 5:20:10 PM
On Mon, 26 Mar 2012 08:11:03 -0400, Dave Angel wrote:

> On 03/26/2012 07:45 AM, redstone-cold@163.com wrote:
>> 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?

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.



-- 
Steven
0
3/27/2012 3:43:46 AM
On Mar 27, 8:43=A0am, Steven D'Aprano <steve
+comp.lang.pyt...@pearwood.info> wrote:
> On Mon, 26 Mar 2012 08:11:03 -0400, Dave Angel wrote:
> > On 03/26/2012 07:45 AM, redstone-c...@163.com wrote:
> >> 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. =A0The input is the same, the output is the sam=
e,
> > what else matters?
>
> def fib1(n):
> =A0 =A0 if n =3D=3D 0: return 0
> =A0 =A0 elif n =3D=3D 1: return 1
> =A0 =A0 f2, f1 =3D 0, 1
> =A0 =A0 for _ in range(2, n+1):
> =A0 =A0 =A0 =A0 f2, f1 =3D f1, f2 + f1
> =A0 =A0 return f1
>
> def fib2(n):
> =A0 =A0 if n =3D=3D 0: return 0
> =A0 =A0 elif n =3D=3D 1: return 1
> =A0 =A0 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.
>
> --
> Steven

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


0
rustompmody (868)
3/27/2012 5:00:48 AM
=E5=9C=A8 2012=E5=B9=B43=E6=9C=8826=E6=97=A5=E6=98=9F=E6=9C=9F=E4=B8=80UTC+=
8=E4=B8=8B=E5=8D=887=E6=97=B645=E5=88=8626=E7=A7=92=EF=BC=8CiMath=E5=86=99=
=E9=81=93=EF=BC=9A
> I know the print statement produces the same result when both of these tw=
o instructions are executed ,I just want to know Is there any difference be=
tween print 3 and print '3' in Python ?

thx everyone
0
iMath
9/9/2012 1:23:37 PM
On 09/09/12 14:23, iMath wrote:
> =E5=9C=A8 2012=E5=B9=B43=E6=9C=8826=E6=97=A5=E6=98=9F=E6=9C=9F=E4=B8=80=
UTC+8=E4=B8=8B=E5=8D=887=E6=97=B645=E5=88=8626=E7=A7=92=EF=BC=8CiMath=E5=86=
=99=E9=81=93=EF=BC=9A
>> 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 differen=
ce between print 3 and print '3' in Python ?
> thx everyone

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

Ian F
0
ian337 (19)
9/9/2012 2:41:46 PM
On Sun, Sep 9, 2012 at 11:33 PM, Dwight Hutto <dwightdhutto@gmail.com> wrot=
e:
>
>
> On Sun, Sep 9, 2012 at 10:41 AM, Ian Foote <ian@feete.org> wrote:
>>
>> On 09/09/12 14:23, iMath wrote:
>>>
>>> =E5=9C=A8 2012=E5=B9=B43=E6=9C=8826=E6=97=A5=E6=98=9F=E6=9C=9F=E4=B8=80=
UTC+8=E4=B8=8B=E5=8D=887=E6=97=B645=E5=88=8626=E7=A7=92=EF=BC=8CiMath=E5=86=
=99=E9=81=93=EF=BC=9A
>>>>
>>>> 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 differ=
ence
>>>> 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 (Inte=
l)]
> on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> exit()
>
> 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.
>>>> import __future__
>>>> x =3D 3
>>>> y =3D '3'
>>>> print(x)
> 3
>>>> print(y)
> 3
>>>>
>>>> type(x)
> <type 'int'>
>>>> type(y)
> <type 'str'>
>
>>>> z =3D '%i' % (3)
>>>> type(z)
> <type 'str'>
>>>>
>
> In other words type(value), and find out the difference.
> --
> Best Regards,
> David Hutto
> CEO: http://www.hitwebdevelopment.com
>

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)] o=
n win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> print 3
3
>>> import __future__
>>> print 3
3
>>> from __future__ import print_function
>>> print 3
  File "<stdin>", line 1
    print 3
          ^
SyntaxError: invalid syntax
0
9/10/2012 6:55:58 AM
On 9/10/2012 2:33 AM, Dwight Hutto wrote:
>
>
> On Sun, Sep 9, 2012 at 10:41 AM, Ian Foote <ian@feete.org
> <mailto:ian@feete.org>> wrote:
>
>     On 09/09/12 14:23, iMath wrote:
>
>         =E5=9C=A8 2012=E5=B9=B43=E6=9C=8826=E6=97=A5=E6=98=9F=E6=9C=9F=E4=
=B8=80UTC+8=E4=B8=8B=E5=8D=887=E6=97=B645=E5=88=8626=E7=A7=92=EF=BC=8C__i=
Math=E5=86=99=E9=81=93=EF=BC=9A
>
>             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.
>  >>> exit()
>
> 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.
>  >>> import __future__
>  >>> x =3D 3
>  >>> y =3D '3'
>  >>> print(x)
> 3
>  >>> print(y)
> 3
>  >>>
>  >>> type(x)
> <type 'int'>
>  >>> type(y)
> <type 'str'>
>
>  >>> z =3D '%i' % (3)
>  >>> type(z)
> <type 'str'>
>  >>>
>
> In other words type(value), and find out the difference.

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

 >>> print(repr(3))
3
 >>> print(repr('3'))
'3'

--=20
Terry Jan Reedy


0
tjreedy (5460)
9/10/2012 7:16:48 AM
Reply: