I have recently started to learn Python and I am hoping that you will be able to help me with a question that has been bothering me. I have been learning Python online with Learn Python The Hard Way. In Exercise 6, I came across a problem where I was using the
x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)
print "I said: %r." % x
print "I also said: %r." % y
I said: 'There are 10 types of people.'.
I also said: "Those who know binary and those who don't.".
%r is getting the
repr version of the string:
>>> x = 'here' >>> print repr(x) 'here'
You see, single quotes are what are normally used. In the case of
y, however, you have a single quote (apostrophe) inside the string. Well, the
repr of an object is often defined so that evaluating it as code is equal to the original object. If Python were to use single quotes, that would result in an error:
>>> x = 'those who don't' File "<stdin>", line 1 x = 'those who don't' ^ SyntaxError: invalid syntax
so it uses double quotes instead.