"foo" and """foo""" are in many respects the same thing; they're both strings. A string on a line by itself is an implicit print command, basically, although I often see print used with the single-quote version and not with the triple-quote version.
The "foo" version doesn't allow you to have quotes -- you can do print "foo"+otherstuff+"bar";, which means that if you want to print quotes within the string, you either need to escape it by using \" instead of " (some sample text), or need to use single-quotes, which don't count as the end-of-string marker ((some sample text)
I'm not sure whether the single-quote version allows newlines -- that is, I know a triple-quote version can start on one line and end fifty lines later; I don't know if a single-quote version can do that or not.
The primary advantage, at least to my mind, of the triple-quote version is that you don't need to escape the quotes. (Especially since I've trained myself into -format HTML tags, and retraining myself to either use ' or \" is annoying.) I'll also often use single quotes for short stretches of code and triple quotes for longer stretches.
I'm not sure whether the single-quote version allows newlines -- that is, I know a triple-quote version can start on one line and end fifty lines later; I don't know if a single-quote version can do that or not.
i'm sure i read somewhere that you couldn't do this with single quotes, but i just tried it, and you can. so who knows...
I definitely can't remember to escape my single-quotes, so I always go ahead and use triple-quotes under the assumption that I'll end up with a single-quote in there somewhere and will throw things when the compiler yells at me for it.
The "foo" version doesn't allow you to have quotes -- you can do print "foo"+otherstuff+"bar";, which means that if you want to print quotes within the string, you either need to escape it by using \" instead of " (some sample text), or need to use single-quotes, which don't count as the end-of-string marker ((some sample text)
I'm not sure whether the single-quote version allows newlines -- that is, I know a triple-quote version can start on one line and end fifty lines later; I don't know if a single-quote version can do that or not.
The primary advantage, at least to my mind, of the triple-quote version is that you don't need to escape the quotes. (Especially since I've trained myself into -format HTML tags, and retraining myself to either use ' or \" is annoying.) I'll also often use single quotes for short stretches of code and triple quotes for longer stretches.
Reply
i'm sure i read somewhere that you couldn't do this with single quotes, but i just tried it, and you can. so who knows...
Reply
Reply
Reply
Leave a comment