Reputation: 577
binary = "binary"
do_not = "don't"
x = "There are %s types of people." % binary
y = "Those who know %s and those who %s." % (binary, do_not)
print "I also said: %r." % y
print "I also said: %r." % x
This is an exercise of Learn Python. I am trying understand '%r' here so I tried the above two prints. The output are
I also asid: "Those who know binary and those who don't.".
I also said: 'There are binary types of people.'.
Now I am wondering why the difference between these two prints is double and single quotes? Thanks for your help.
Upvotes: 1
Views: 714
Reputation: 107287
Its because if don't
that use a single quotes , if a string contain single quotes python automaticaly put it in double quotes "
. else it put the string in a single quote .
>>> x
'There are binary types of people.'
>>> y
"Those who know binary and those who don't."
Note : you cant put string that contain single quote in single quote it raise a syntax error
:
>>> 'Those who know binary and those who don't.'
File "<stdin>", line 1
'Those who know binary and those who don't.'
^
SyntaxError: invalid syntax
and %r
converts any Python object using repr()(Return a string containing a printable representation of an object.)).
This is the same value yielded by conversions (reverse quotes). It is sometimes useful to be able to access this operation as an ordinary 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(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a __repr__() method.
>>> repr("example")
"'example'"
>>> repr("exampl'e")
'"exampl\'e"'
read more about String Formatting
Upvotes: 0
Reputation: 8956
The %r
format causes Python to print using the repr
function. So the following are equivalent:
"%r" % my_string ≡ "%s" % repr(my_string)
So then the question becomes, why does repr
sometimes use single quotes and sometimes uses double quotes? Well, first of all, the goal of repr
is to produce a representation of a Python object that is also valid Python source code (this is not mandatory requirement but for many Python types it's indeed valid). For a string that contains an apostrophe in it, there are multiple ways to represent it:
"Don't" ≡ 'Don\'t'
repr
decides to be "smart" and picks the one that is easier to read, namely using double quotes, as long as it doesn't already contain double quotes. In all other cases, Python falls back to the default single quotes. This is why you see different behavior for different strings.
More relevantly, don't rely on repr
(or %r
) to quote strings for human consumption, write your own function for that. It's not meant to for that, it's mostly used as a convenient way of dumping a Python object when debugging.
AFAIK there's no documentation that clearly stipulates as to what you will get when you repr
a string, except that eval(repr(my_string)) == my_string
is expected to hold true.
Upvotes: 6
Reputation: 700
So let me give you an example
print "Let's try this example"
The following example will print fine but if you use a single quote, it will exit after the ' of let's.
Here's another example
print 'He said "Hello!"'
If you use a double quotes here, then it will escape before the Hello!. Those are simple use cases. They come in handy if you're using things like output html tags that need single or double quotes.
Upvotes: 0