[Tutor] Why difference between printing string & typing its object reference at the prompt?
Brian van den Broek
brian.van.den.broek at gmail.com
Wed Oct 3 07:00:30 CEST 2012
On 2 Oct 2012 23:17, "boB Stepp" <robertvstepp at gmail.com> wrote:
> I am puzzled by the results of the following:
> >>> x = "Test"
> >>> x
> >>> print(x)
> I understand that 'Test' is the stored value in memory where the
> single quotes designate the value as being a string data type. So it
> makes sense to me that just typing the object reference for the string
> results in including the single quotes. But why does the print() strip
> the quotes off? Is just as simple as
Under the covers, in python 2.x, print x causes the human readable string
representation of x to be output by calling x.__str__. In an interactive
prompt, typing x displays the python representation of x by calling
x.__repr__. These can be the same or quite similar or quite different.
When possible, __repr__ special methods ought to be defined so x equals
I believe, but don't warrant that in this regard python 3.x behave like 2.x
(modulo the difference in the print syntax).
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor