Wondering if it’s my code or…? returning inconsistent usage of single/double quotation marks and why they’re there in some instances and not others…
I’m using Python3 and not very familiar with the old string formatters but I think it will always output the reverse quotation mark. If you used double quotation marks it will output single quotation marks and vice versa.
Your code and the resulting output look perfectly correct.
%r comes across a string it normally surrounds the string with single quotemarks, i.e. lines 7, 10 and 13.
Line 7 is pretty straightforward … four separate simple strings.
Line 10 deals with the value of the variable named
formatter, which itself is a string.
Line 13 is an interesting one … notice that the 3rd line of the poem already contains a single quotemark in the word
didn't? So in this case, the string is surrounded by double quotemarks instead.
Finally, lines 5 and 8 are not strings, so are not surrounded by any quotemarks at all.
Hope that helps,
Thank you, yes; I learned the reverse output of single/double quotation marks in Ex10.
Thank you Phil! Your explanation of line 13 is a suggestion I was looking for. Explicit coding is the key to generating desired output, which is why this funny little switcheroo is bugging me… I reran the file with a backslash single quote in “don’t” of line 13 thinking that’d make single quotes for each poem line in output, but alas, nope. I’m fixated on understanding why the discrepancy is happening and how to use it, avoid it, or accept it. Does Python (I’m using 2, by the way) have a mind of its own and simply want to contribute its own flair? Sorry; I’m obsessing here…I want to understand why that third poem line outputs double quotes instead of single… I’ve even run the code using single quotes and backslash single for that particular string bit and still it outputs doubles. Mysterious…
Remember Exercise 1?
Look at line 6 in Exercise 1 where the string is surrounded by double quotemarks, rather than single quotemarks like some of the other strings. Why would you get a syntax error if you used single quotemarks for this particular string? (Exercise 10 gives you the answer!).
You can surround strings with either single or double quotemarks… unless the string itself contains a single or double quotemark, in which case you must use single quotemarks if it contains a double quotemark and double quotemarks if it contains a singe quotemark. (Or you could use the relevant escape sequence inside the string instead; again see Exercise 10).
What you’re seeing when you use the
%r format character is the result of a special little piece of built-in Python code called
repr(). The official explanation of
repr() is here (https://docs.python.org/2/library/functions.html#repr), but that’s a bit complicated. Have a look at the ‘Common Student Questions’ section for Exercise 8 for some more appropriate explanations.
Ah…the content of “formatter” variable is the “%r” that “represents” and is not “pretty” but for inspecting and debugging… whereas, when I run the third “%r” in the first line assigning input as “%s”, the output removes quotes unless I add in the escape to force print them. Excellent!! Thanks a million! More light bulbs are going off in my head! Thank you thank you thank you! Especially for sending me back to Ex 1. That whole block of code in English, makes more sense to me now. Again, thank you! Such a novice am I, but quite capable of learning and bloody determined, hence my dog-with-a-bone focus; Now I can move on and see more as more light bulbs turn on! :)
Python doesn’t differentiate between single and double quotes on string literals so its a matter of style really.
However, some languages like Ruby I believe, are more efficient if you use single quotes rather than doubles. This is due to the syntax implying that single quotes are just strings whereas doubles may contain string interpolation (inserted variables into the string body), so have to pre-scan the string. This is has a performance impact.
Hi @Tulia, everyone’s answers here are good. The only thing I’d add is this is how Python would do it. It doesn’t remember exactly how you wrote the string, instead it takes the internal version of the string, then writes it out in a way that will still work the same, but doesn’t look the same as yours. It’s nothing to worry about.