Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #22203
| Subject | Re: Is there any difference between print 3 and print '3' in Python ? |
|---|---|
| From | "J. Cliff Dyer" <jcd@sdf.lonestar.org> |
| References | <5128580.32.1332762326119.JavaMail.geo-discussion-forums@pbom7> |
| Date | 2012-03-26 13:06 -0400 |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1014.1332781633.3037.python-list@python.org> (permalink) |
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 ?
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Is there any difference between print 3 and print '3' in Python ? redstone-cold@163.com - 2012-03-26 04:45 -0700
Re: Is there any difference between print 3 and print '3' in Python ? Chris Angelico <rosuav@gmail.com> - 2012-03-26 23:01 +1100
Re: Is there any difference between print 3 and print '3' in Python ? Kiuhnm <kiuhnm03.4t.yahoo.it> - 2012-03-26 14:07 +0200
Re: Is there any difference between print 3 and print '3' in Python ? Robert Kern <robert.kern@gmail.com> - 2012-03-26 13:10 +0100
Re: Is there any difference between print 3 and print '3' in Python ? Dave Angel <d@davea.name> - 2012-03-26 08:11 -0400
Re: Is there any difference between print 3 and print '3' in Python ? redstone-cold@163.com - 2012-03-26 07:28 -0700
Re: Is there any difference between print 3 and print '3' in Python ? Stefan Behnel <stefan_ml@behnel.de> - 2012-03-26 17:03 +0200
Re: Is there any difference between print 3 and print '3' in Python ? redstone-cold@163.com - 2012-03-26 07:28 -0700
Re: Is there any difference between print 3 and print '3' in Python ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-27 03:43 +0000
Re: Is there any difference between print 3 and print '3' in Python ? rusi <rustompmody@gmail.com> - 2012-03-26 22:00 -0700
Re: Is there any difference between print 3 and print '3' in Python ? Terry Reedy <tjreedy@udel.edu> - 2012-03-26 11:45 -0400
Re: Is there any difference between print 3 and print '3' in Python ? "J. Cliff Dyer" <jcd@sdf.lonestar.org> - 2012-03-26 13:06 -0400
Re: Is there any difference between print 3 and print '3' in Python ? Peter Otten <__peter__@web.de> - 2012-03-26 19:20 +0200
csiph-web