183

On Learn Python the Hard Way page 21, I see this code example:

x = "There are %d types of people." % 10
...
print "I said: %r." % x

Why is %r used here instead of %s? When would you use %r, and when would you use %s?

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
coffee-grinder
  • 26,940
  • 19
  • 56
  • 82

4 Answers4

351

The %s specifier converts the object using str(), and %r converts it using repr().

For some objects such as integers, they yield the same result, but repr() is special in that (for types where this is possible) it conventionally returns a result that is valid Python syntax, which could be used to unambiguously recreate the object it represents.

Here's an example, using a date:

>>> import datetime
>>> d = datetime.date.today()
>>> str(d)
'2011-05-14'
>>> repr(d)
'datetime.date(2011, 5, 14)'

Types for which repr() doesn't produce Python syntax include those that point to external resources such as a file, which you can't guarantee to recreate in a different context.

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Ben James
  • 121,135
  • 26
  • 193
  • 155
  • 52
    I love this example, because it shows a big difference between %s and %r ... most examples just show something like 'apple' vs. apple, and then it's not clear why we even have %r. – macloo Jan 11 '13 at 23:35
  • 1
    How would one implement the % specifier in Python 3.3? I am also following the Learn Python the Hard Way sequence, but have found that there are a number of syntax differences between the Python versions. – nitrl Jul 18 '13 at 18:51
  • Thanks. I was wondering why one might use the %r - but I now understand from your example above. – Helen Neely Jan 16 '14 at 15:49
  • 3
    An important difference when using these in string formatting is that %r will also include the delimiting quotes. print('Text: %r' % 'Hello') -> Text: 'Hello', print('Text: %s' % 'Hello') -> Text Hello – Markus Jan 10 '18 at 17:16
20

Use the %r for debugging, since it displays the "raw" data of the variable, but the others are used for displaying to users.

That's how %r formatting works; it prints it the way you wrote it (or close to it). It's the "raw" format for debugging. Here \n used to display to users doesn't work. %r shows the representation if the raw data of the variable.

months = "\nJan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
print "Here are the months: %r" % months

Output:

Here are the months: '\nJan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug'

Check this example from Learn Python the Hard Way.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
Pramod Bhat
  • 568
  • 4
  • 9
16

%r shows with quotes:

It will be like:

I said: 'There are 10 types of people.'.

If you had used %s it would have been:

I said: There are 10 types of people..
manojlds
  • 290,304
  • 63
  • 469
  • 417
14

This is a version of Ben James's answer, above:

>>> import datetime
>>> x = datetime.date.today()
>>> print x
2013-01-11
>>> 
>>> 
>>> print "Today's date is %s ..." % x
Today's date is 2013-01-11 ...
>>> 
>>> print "Today's date is %r ..." % x
Today's date is datetime.date(2013, 1, 11) ...
>>>

When I ran this, it helped me see the usefulness of %r.

macloo
  • 627
  • 1
  • 9
  • 19