43

http://learnpythonthehardway.org/book/ex6.html

Zed seems to use %r and %s interchangeably here, is there any difference between the two? Why not just use %s all the time?

Also, I wasn't sure what to search for in the documentation to find more info on this. What are %r and %s called exactly? Formatting strings?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
some1
  • 2,447
  • 8
  • 26
  • 23
  • 1
    see http://stackoverflow.com/a/6005180/1114171 and http://stackoverflow.com/a/997807/1114171, it is kown as a formatting / interpolation operator see [docs](http://docs.python.org/library/stdtypes.html#string-formatting-operations) – T I Jan 24 '12 at 11:40
  • possible duplicate of [what's the meaning of %r in python](http://stackoverflow.com/questions/2354329/whats-the-meaning-of-r-in-python) – Jon Cage Jan 24 '12 at 11:45
  • 2
    possible duplicate of [When to use %r instead of %s in Python?](http://stackoverflow.com/questions/6005159/when-to-use-r-instead-of-s-in-python) – jscs Jan 25 '12 at 04:54

7 Answers7

67

They are called string formatting operations.

The difference between %s and %r is that %s uses the str function and %r uses the repr function. You can read about the differences between str and repr in this answer, but for built-in types, the biggest difference in practice is that repr for strings includes quotes and all special characters are escaped.

Community
  • 1
  • 1
shang
  • 24,642
  • 3
  • 58
  • 86
21

%r calls repr, while %s calls str. These may behave differently for some types, but not for others: repr returns "a printable representation of an object", while str returns "a nicely printable representation of an object". For example, they are different for strings:

>>> s = "spam"
>>> print(repr(s))
'spam'
>>> print(str(s))
spam

In this case, the repr is the literal representation of a string (which the Python interpreter can parse into a str object), while the str is just the contents of the string.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
3

The following is a summary of the preceding three code examples.

# First Example
s = 'spam'
# "repr" returns a printable representation of an object,
# which means the quote marks will also be printed.
print(repr(s))
# 'spam'
# "str" returns a nicely printable representation of an
# object, which means the quote marks are not included.
print(str(s))
# spam

# Second Example.
x = "example"
print ("My %r" %x)
# My 'example'
# Note that the original double quotes now appear as single quotes.
print ("My %s" %x)
# My example

# Third Example.
x = 'xxx'
withR = ("Prints with quotes: %r" %x)
withS = ("Prints without quotes: %s" %x)
print(withR)
# Prints with quotes: 'xxx'
print(withS)
# Prints without quotes: xxx
S. Newton
  • 31
  • 2
2

%s invokes str(), whereas %r invokes repr(). For details, see Difference between __str__ and __repr__ in Python

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0
 x = "example"
 print "My %s"%x

       My example

 print "My %r"%x

       My 'example'

It is well explained in the above answers. I have tried to show the same with a simple example.

misguided
  • 3,699
  • 21
  • 54
  • 96
0

%s => string

%r => exactly as is

Using the code in the book:

my_name = 'Zed A. Shaw'
print "Let's talk about %s." % my_name
print "Let's talk about %r." % my_name

we get

Let's talk about Zed A. Shaw.
Let's talk about 'Zed A. Shaw'.
Snowcrash
  • 80,579
  • 89
  • 266
  • 376
-2

The code below illustrates the difference. Same value prints differently:

x = "xxx"
withR = "prints with quotes %r"
withS = "prints without quotes %s"
Default
  • 16,020
  • 3
  • 24
  • 38