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

Steven D'Aprano steve at pearwood.info
Thu Oct 4 13:28:32 CEST 2012


On 04/10/12 13:39, boB Stepp wrote:

>> But not always. For example:
>>
>> py>  from decimal import Decimal as D
>> py>  x = D("1.23")
>> py>  print(str(x))
>> 1.23
>> py>  print(repr(x))
>> Decimal('1.23')
>
> These contrasting examples are very illuminating. So in print(str(x))
> the object, D("1.23"), is being converted into a readable string,
> which makes the most sense as 1.23. But print(repr(x)) is giving a
> string representation of the object as code, which is more than just
> 1.23, the Decimal('1.23'). Am I understanding this correctly?

Pretty close.

In the example above, the calls to print are only there to avoid
distracting you with the string delimiters, the outer quote marks. It's
str() and repr() that are doing the real work.

Apart from that, you've got it right. str(x) returns a human-readable
version of x, which in this case is "1.23" (excluding the quote marks,
of course). The designer of the Decimal class choose for repr() of a
decimal to look as much as possible like the call to the class that
created the object in the first place. (Or at least an equivalent
call.) In this case, that is "Decimal('1.23')".


>> Unfortunately, the difference between str() and repr() is kind of
>> arbitrary and depends on the object. str() is supposed to return a
>> "human-readable" version of the object, for display, while repr() is
>> supposed to return a string which would work as code, but those are more
>> guidelines than hard rules.
>
> Will these fine distinctions be easy for me to pick up on as I
> progress in my Python studies? I suspect that I am going to have to
> experiment with str() and repr() in each new situation to see what
> results.

*shrug* I've been programming in Python for over 10 years, and I still
forget when str() is used and when repr() is used. I always have to check.
But maybe that's just me.

Remember, there is no hard rule that tells you what the output of str()
and repr() must be (apart from strings). Different programmers have
different ideas of what is useful, meaningful, or possible.


[...]
>> But repr() of a string creates a new string showing the representation
>> of the original string, that is, what you would need to type in source
>> code to make that string. That means:
>>
>> 1) wrap the whole thing in delimiters (quotation marks)
>> 2) escaping special characters like tabs, newlines, and binary
>>     characters.
>
> As to point 2), will repr() insert "\" (I am assuming Python uses a
> backslash like other languages to escape. I have not read about this
> in Python yet.) for these special characters? Will str() do the same?

Yes to repr(), no to str().

Remember, str() of a string is just the same string unchanged. If the
input string contains a newline, the output will also contain a newline:

py> s = "abc" + chr(10) + "def"
py> print(s)
abc
def
py> print(str(s))
abc
def


But repr() will create a new string, and escape any "non-printable"
character (and a few which are printable):

py> print(repr(s))
'abc\ndef'


So this shows us that instead of creating string s as I did above, by
concatenating two substrings and a newline character, I could just as
easily have created it in one go using a \n escape:

py> t = "abc\ndef"
py> s == t
True


Notice too that there is no difference between the two different
flavours of single quote delimiters. Whether you write "a" or 'a'
is entirely a matter of personal preference. Python accepts both to
make it easy to input strings containing quote marks:

s = "this string contains ' a single-quote"
t = 'this string contains " a double-quote'


Such single quote strings must start and end on the same line. On the
other hand, *triple-quote* delimiters """ or ''' are used for
multiline strings. They can extend over multiple lines.


Now, ask me about *raw strings*, and the difference between Unicode
and byte strings :)


-- 
Steven


More information about the Tutor mailing list