Blackacre
Blackacre

Reputation: 23

Python: Why does %r not accurately represent the raw data on \" versus \'

I've been going through Zed's LPTHW and I have been messing around with escape characters after doing lesson 10. While fooling around with %r I came across this, and I have no idea why it's happening (I'm so new to any form of programming/coding it hurts):

test = "10'5\""
test_2 = '10\'5"'

print "%r" % test
print "%r" % test_2 

When I run this, I get:

'10\'5"'
'10\'5"'

I'm confused. I had assumed that I would get output in the following:

"10'5\"'
'10\'5"'

It was my understanding that %r would return the string identical to how it is written, yet it seems to convert it to test_2 by moving the \ to the left.

Am I missing something here?

Thanks.

Upvotes: 1

Views: 44

Answers (2)

Fokhruz Zaman
Fokhruz Zaman

Reputation: 23

Python string literals or constants can start either with a single-quote(') or a double-quote("). When it begins with a single-quote('), then it must end with a with a single-quote(').

And the similar logic goes for a string literal or constant beginning with with a double-quote("). If it begins with a double-quote ("), then it must end with the same double-quote(").

Now the interesting thing to notice here is: if a string literal or constant begins with a double-quote("), then the single-quote(') inside that string literal is just another character. You don't need to put the escape character (\) to tell Python interpreter to retain that single-quote(') intact.

And the similar logic goes for a string literal or constant that begins with a single-quote('), then the double-quote(") inside that string literal is just another character. You don't need to put the escape character (\) to tell Python interpreter to retain that double-quote(") intact.

So in your following code fragment, you initialized your variable test with a string literal or constant. You defined your string constant by the double- quotes("..."). Inside your string constant, you put two digits (10), a single- quote('), digit(5), and a double-quote (which needed escaping by the escape character).

You did the same thing for your variable test_2 in your following code fragment. But here you defined your string literal or constant using the single-quotes('...'), so you needed to escape the single-quote(') after the first two digits(10).

test = "10'5\""   
test_2 = '10\'5"'

print "%r" % test
print "%r" % test_2 

If you print your variables using the print format %s instead of the raw format %r as following, you will get the same string literal or constant value for both the variables, which is: 10'5"

print "%s" % test
print "%s" % test_2 

But for the raw format %r value of your two Python string variables test and test_2, the Python interpreter internally chose to represent your raw string value beginning and ending with a single-quote, and printing both as : '10\'5"'. This has no bearing on how you defined your string literals, using either the double-quotes("...") or the single-quotes('...').

Upvotes: 0

user149341
user149341

Reputation:

It was my understanding that %r would return the string identical to how it is written

Your understanding is incorrect. Python does not "remember" how a string was written in the source code; all that matters to the interpreter is that it contains the characters:

10'5"

Printing a repr of that string will use whichever type of quotation marks Python feels is most appropriate for its contents. Since both strings contain the same characters, they are printed identically by repr (and, hence, by the %r format string).

Upvotes: 5

Related Questions