repr on a string

David Shochat shochatd at yahoo.com
Sun Jun 1 16:16:41 EDT 2003


The Library Reference says this about built-in repr():
 Return a string containing a printable representation of an object.

While studying Programming Python, 2nd Ed. Example 2-18, p. 84, I was
wondering what the point was of applying repr to a component (dir) of 
sys.path. Isn't it after all, already a printable string? Ok, so 
what does it mean to take repr of a printable string, and why does 
the author want to here? Are we worried that the path contains an 
unprintable directory name or something?

I tried this:
>>> str = 'dog'
>>> str
'dog'
>>> repr(str)
"'dog'"

We now seem to have a string with quote characters as its first 
and last components. I would have thought that the operation of 
making a printable representation of something that is already 
printable would be the identify function. Could someone
explain precisely what is going on here?
Thanks.
-- David





More information about the Python-list mailing list