4

So I have this code:

    formatter = "%r %r %r %r"

print formatter % (1, 2, 3, 4)
print formatter % ("one", "two", "three", "four")

And I get this output:

1 2 3 4

'one' 'two' 'three' 'four'


My question is:

Why does the second line of output have single quotes around it? I'm not quite sure how the %r conversion type really works.

When I change the code to:

formatter = "%r %r %r %r"

print formatter % (1, 2, 3, 4)
print "%s %s %s %s" % ("one", "two", "three", "four")

I get this result:

1 2 3 4

one two three four

I just don't understand why they work differently. Can someone break it down for me?


I've read:

http://docs.python.org/library/stdtypes.html &

http://docs.python.org/library/functions.html#repr

Dorje
  • 173
  • 2
  • 10

2 Answers2

2

With the expression 'abc%rdef' % obj , the part '%r' is replaced with repr(obj)

With the expression 'ABC%sDEF' % obj , the part '%s' is replaced with str(obj)

.

repr() is a function that , for common objects, returns a string that is the same as the one you would write in a script to define the object passed as argument to the repr() function:

For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval() http://docs.python.org/library/functions.html#repr

.

Example 1

if you consider the list defined by li = [12,45,'haze']

print li will print [12,45,'haze']

print repr(li) will also print [12,45,'haze'] , because [12,45,'haze'] is the sequence of characters that are written in a script to define the list li with this value

Example 2

if you consider the string defined by ss = 'oregon' :

print ss will print oregon , without any quote around

print repr(ss) will print 'oregon' , since 'oregon' is the sequence of characters that you must write in a script if you want to define the string ss with the value oregon in a program

.

So, this means that , in fact, for common objects, repr() and str() return strings that are in general equal, except for a string object. That makes repr() particularly interesting for string objects. It is very useful to analyse the contents of HTML codes, for exemple.

eyquem
  • 26,771
  • 7
  • 38
  • 46
  • Ooooo! And the lightbulb goes on. This is much appreciated. – Dorje Aug 28 '11 at 13:59
  • Do you have an examples of how it's "useful to analyse the contents of HTML codes." – Dorje Aug 28 '11 at 14:01
  • @Dorje Yes. Read first this post: (http://stackoverflow.com/questions/7213970/python-url-variable-int-add-to-string/7214095#7214095) In the second part of the post, there is this instruction ``li = htmlcode.splitlines(True)`` followed by this other useful instruction ``print '\n'.join(str(i) + ' ' + repr(line)+'\n' for i,line in enumerate(li) if 275 – eyquem Aug 28 '11 at 14:20
  • @Dorje More complex and more in relation to your question is this post (http://stackoverflow.com/questions/7206143/python-regex-problem/7207746#7207746) – eyquem Aug 28 '11 at 14:21
  • @Dorje By the way, re-reading the docs concerning **repr()**, it reminded me that we can write ``print `x` `` instead of ``repr(x)`` – eyquem Aug 28 '11 at 14:27
  • @Dorje And I realize that it is better to write ``print '\n'.join('%s %r\n' % (i,line) for i,line in enumerate(li)`` . Or even like that: ``print '\n'.join(map(lambda x:'%s %r\n' % x, enumerate(ch.splitlines(True))))`` – eyquem Aug 28 '11 at 14:35
  • Wow. I can see why that's useful. Everything you've add here will be useful for me to pull apart and learn. Thanks a lot. – Dorje Aug 28 '11 at 22:54
0

%s tell's python to call the str() function on each element of your tuple. %r tell's python to call the repr() function on each element of your tuple.

By the docs:

str():

Return a string containing a nicely printable representation of an object. For strings, this returns the string itself.

repr():

Return a string containing a printable representation of an object.

This means, if the object you call repr() on is a string object (in python everything is an object) it shows you how the different characters are represented.

@your specific question: I assume that the "..." indicate, that it is a string. If you call repr() on an int there are no "...".

Aufwind
  • 25,310
  • 38
  • 109
  • 154
  • Thanks for the explanation. This really helped explain the docs more. "This means, if the object you call repr() on is a string object (in python everything is an object) it shows you how the different characters are represented." – Dorje Aug 28 '11 at 13:56