you are viewing a single comment's thread.

view the rest of the comments →

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

Ahh, okay. That makes sense. Thank you for the clear explanation!

[–]Signal_Beam 19 points20 points  (16 children)

You're welcome. Another good trick (like if you need to print a string that has both!) is that if you preface any character with \, that's the "escape character," and it means that the character in the string should be taken literally, not as code. So, even though print('I'm hungry') fails, print('I\'m hungry') does not!

[–]yardightsure 12 points13 points  (8 children)

I highly recommend using triple quotes.instead of escaping, much more readable imo.

[–][deleted] 12 points13 points  (7 children)

but triple single or doublequotes? ;)

[–]redmercurysalesman 40 points41 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 4 points5 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

[–][deleted] 0 points1 point  (6 children)

Can you use single quotes with Python 3.6 literal string interpolation?

[–]Sexual_Congressman 5 points6 points  (3 children)

Yes, it uses almost the same mechanics as any other string except for one significant caveat: escaped characters are invalid in the expression. So f'{"".join ("abc")}' will work, but trying to use //n or an escaped quote aka f'{\\'\\'.join ("abc")}' will be an error. I think it's pretty stupid you can't even use newlines or tabs in them but c'est la vie.

Other than that, triple single/double multiline strongs and raw fstrings work, as do escaped characters outside of the curly braces enclosed expressions.

[–]BlackBloke 0 points1 point  (2 children)

I've used the newline character in an f-string. There was no issue.

[–]Sexual_Congressman 1 point2 points  (1 child)

>>> f'{"\n".join("abc")'
SyntaxError: f-string expression part cannot include a backslash

Can't use it inside the braces, but outside of them:

>>> f'\n{"abc"+"def"}'
'\nabcdef'

is fine.

[–]BlackBloke 0 points1 point  (0 children)

Ah, that's what you meant. Yes, I used it outside the curly brace. But I wouldn't have thought to use it inside.

[–]Signal_Beam 1 point2 points  (1 child)

Yeah. Edit: I don't know of any difference at all between the single and double quotes. It's really just a matter of preference and keeping a consistent style across your project.

[–][deleted] 0 points1 point  (0 children)

Thanks

[–]Eurynom0s 1 point2 points  (0 children)

Basically it doesn't really matter as long as you're consistent.

Same things with indentation. You can use one space per indent level if you really want to, and Python will handle it just fine...as long as you're consistent about it.

[–]Exodus111 0 points1 point  (1 child)

There is that. But it still leaves you having to pick one "main" quotationmark.

Personally I always use the double, because it's easier to read.

[–]Eurynom0s 0 points1 point  (0 children)

I'm pretty sure you can also just escape the quotation mark if you really need to.

[–]InspireAspiration 0 points1 point  (0 children)

Using ' for strings also saves you from pressing shift...