7

Possible Duplicate:
Single quotes vs. double quotes in Python

I have seen that when i have to work with string in Python both of the following sintax are accepted:

mystring1  = "here is my string 1"
mystring2  = 'here is my string 2'

Is anyway there any difference?

Is it by any reason better use one solution rather than the other?

Cheers,

Community
  • 1
  • 1
Stefano
  • 3,981
  • 8
  • 36
  • 66

7 Answers7

6

No, there isn't. When the string contains a single quote, it's easier to enclose it in double quotes, and vice versa. Other than this, my advice would be to pick a style and stick to it.

Another useful type of string literals are triple-quoted strings that can span multiple lines:

s = """string literal...
...continues on second line...
...and ends here"""

Again, it's up to you whether to use single or double quotes for this.

Lastly, I'd like to mention "raw string literals". These are enclosed in r"..." or r'...' and prevent escape sequences (such as \n) from being parsed as such. Among other things, raw string literals are very handy for specifying regular expressions.

Read more about Python string literals here.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 1
    One confusion about raw strings is that people sometimes think they are a special kind of string. They aren't, the leading 'r' just tells the interpreter to do a different kind of processing for the literal that follows, treating backslashes just like ordinary characters. The result is a plain old ordinary string. The "raw" part is the literal, not the result. So I always use the term "raw string literals", I think this focuses the rawness on the literal, not on the string that you get back. – PaulMcG Oct 28 '11 at 12:37
2

While it's true that there is no difference between one and the other, I encountered a lot of the following behavior in the opensource community:

  • " for text that is supposed to be read (email, feeback, execption, etc)
  • ' for data text (key dict, function arguments, etc)
  • triple " for any docstring or text that includes " and '
Bite code
  • 578,959
  • 113
  • 301
  • 329
  • I use `'''` all the time for docstrings, since they require one keystroke less to type. I hope that doesn't make my projects any less serious :) – Xion Oct 28 '11 at 12:34
  • There is no reason not to. I just never saw it. But it's only less keystrokes on the QWERTY keyboard, not every keyboards in the world. On my keyboard, '"' is easier to use. – Bite code Oct 28 '11 at 12:42
  • Lol. I just grepped the Python source code. It's full of `'''`. Ok, I guess I have to remove that assertion :-p – Bite code Oct 28 '11 at 12:45
  • +1 for the idea of using `'` and `"` based on destination, not to say that it's really useful, but I like the concept and will see what I can do with it :-) – Joël Oct 28 '11 at 13:53
1

No. A matter of style only. Just be consistent.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

I tend to using " simply because that's what most other programming languages use.

So, habit, really.

Dave
  • 6,064
  • 4
  • 31
  • 38
  • And I prefer `'` because in my keyboard it doesn't need the Shift key, while `"` needs. It is really a matter of habit! – heltonbiker Oct 28 '11 at 12:23
1

There's no difference.

What's better is arguable. I use "..." for text strings and '...' for characters, because that's consistent with other languages and may save you some keypresses when porting to/from different language. For regexps and SQL queries, I always use r'''...''', because they frequently end up containing backslashes and both types of quotes.

hamstergene
  • 24,039
  • 5
  • 57
  • 72
1

Python is all about the least amount of code to get the most effect. The shorter the better. And ' is, in a way, one dot shorter than " which is why I prefer it. :)

Björn Lindqvist
  • 19,221
  • 20
  • 87
  • 122
0

As everyone's pointed out, they're functionally identical. However, PEP 257 (Docstring Conventions) suggests always using """ around docstrings just for the purposes of consistency. No one's likely to yell at you or think poorly of you if you don't, but there it is.

Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65