1

Why isn't the output of x single-quoted like y? My speculation so far is that because there's a single-quote inside x it makes it awkward if it would be printed with single-quotes. How can I prevent that from happening? Does it have to do something with escape sequences?

x = "-Why's that?"

y = "-What are you talking about dude?"

print ("%r\n%r")% (x, y)

Output:

"-Why's that?"

'-What are you talking about dude?'
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    *"because there's a single-quote inside the "x" string makes it awkward if it would be printed with single-quotes"* - exactly. *"How can I prevent that from happening?"* - why do you want to? – jonrsharpe Jul 24 '16 at 22:02
  • AS you have said in your question the reason for the x needing double-quotes is because of the single quote in the string. As you have suggested you can use escape sequences instead – Anthony Staunton Jul 24 '16 at 22:02
  • also, using `%r` means it prints the raw string, so it's just using what you used. If you use `%s` it should print just the string itself. – Toby Jul 24 '16 at 22:03
  • Could you [edit] your question to explain exactly what result you're trying to achieve? – Dan Getz Jul 24 '16 at 22:07
  • This is not the best duplicate, so here are a few others: [double quotes in string representation](http://stackoverflow.com/q/11917043/216074), [Understanding repr( ) function in Python](http://stackoverflow.com/q/7784148/216074), [Force repr() to use single quotes](http://stackoverflow.com/q/27402168/216074). Basically `'%r' % something` calls the `repr()` function to get a string representation for `something`. For strings, `repr()` will either use single or double quotes. – poke Jul 24 '16 at 22:10
  • I just want to know why it's printed like this. But I think I got my question answered. – Chris Tzikas Jul 24 '16 at 22:12

0 Answers0