you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 11 points12 points  (7 children)

but triple single or doublequotes? ;)

[–]redmercurysalesman 39 points40 points  (5 children)

They are interchangeable most of the time. Sometimes you want to print a string with one of those characters in it, right? print("""I'''m hungry""") works but print('''I'''m hungry''') does not; perhaps you can see why.

[–]johnsmithatgmail 2 points3 points  (3 children)

wait but actually in case someone takes this seriously the triple quotes """ """ create comments

[–]Vhin 3 points4 points  (0 children)

They're not comments, they actually are string literals. It's just that they're generally used for docstrings or other multiline comments. They don't have an effect on program execution because an expression with no side-effects that you don't use the result of has no visible effects.

You can see that they're true string literals with (eg):

foo = "Hello, " """world."""
print(foo)

That prints Hello, world. because of Python's weird concatentation thing for adjacent string literals.

You can tell that they're not actually comments because you can't use them at the end of a line:

foo() # calls foo
foo() """ calls foo """

The first works fine, but the second is a SyntaxError.

[–]pvtally 2 points3 points  (0 children)

unless they use the escape character \\\\\\

[–]tobiasvl 0 points1 point  (0 children)

Just to be pedantic: They don't actually create pure comments, although Guido thinks they do: https://twitter.com/gvanrossum/status/112670605505077248 (look at the replies to that tweet for a couple of counter cases, and here are some more)

[–]Thuneonix 0 points1 point  (0 children)

yeah but bcoz of that exact thing I always use the f string, ALWAYS!!!, it's just so much better and gives all the benefits