[Tutor] Why difference between printing string & typing its object reference at the prompt?

Dave Angel d at davea.name
Wed Oct 3 06:53:08 CEST 2012


On 10/02/2012 11:15 PM, boB Stepp wrote:
> After much diddling around I have finally settled on a text to study
> (Programming in Python 3, 2nd edition, by Mark Summerfield) and have
> defaulted to using IDLE, deferring worrying about editors/IDEs until I
> feel comfortable in Python.
>
> I am puzzled by the results of the following:
>
>>>> x = "Test"
>>>> x
> 'Test'
>>>> print(x)
> Test
>
> 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 normally people when performing a
> print just want the unadorned text, so that is the behavior built into
> the print function? Or is there something more subtle going on that I
> am totally missing? If an explanation is in one of my several books,
> it is currently eluding me.
>

There are two operations supported by (most) objects that produce a
string.  One is exemplified by the str() function, which converts an
object to a string.  That's the one called implicitly by print().  This
form just represents the data, in the form most likely to be needed by
the end user.

The other operation is repr(), which attempts to produce a string that
could be used in a program to reproduce the actual object.  So a repr()
will have quote marks artificially added, or brackets, or commas, or
whatever seems appropriate for the particular object.  This is intended
for the programmer's use, not for the end user.

When you program  x = "Test", the string object that is created does not
have quote marks in it anywhere.  It also doesn't care whether you
produced it by single quotes, double quotes, triple quotes, or by some
manipulation of one or more other objects.  It has 4 characters in it. 
Period.

If you take that same string and do a repr() on it, it will produce
another string that does have some form of quotes, though not
necessarily the ones used originally.

In the interactive interpreter (I've never used IDLE), entering in an
expression without assigning it to anything will cause the result of the
expression to be displayed with repr().

Your question was about string objects, but I tried to make the
explanation as generic as possible.  Those two functions, str() and
repr(), are used, or implied in many places.  For example, if you print
a list, it'll call str() on the whole list.  But the list object's logic
will in turn call repr() on each of its elements, and put the whole
thing together with braces and commas.

(Finer detail:  There are special methods in the class for each object,
__str__() and __repr__(), which actually have the code.  But you should
never call them directly, so you won't need to know about them till you
start building your own classes)

-- 

DaveA



More information about the Tutor mailing list