Should i use single or double quotes in python?
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other? asked Sep 11, 2008 at 8:18
readonlyreadonly 332k107 gold badges203 silver badges204 bronze badges 0 I like to use double quotes around strings that are used for interpolation or that are natural language messages, and single quotes for small symbol-like strings, but will break the rules if the strings contain quotes, or if I forget. I use triple double quotes for docstrings and raw string literals for regular expressions even if they aren't needed. For example:
answered Sep 11, 2008 at 10:06
Will HarrisWill Harris 21.5k11 gold badges64 silver badges63 bronze badges 7 Quoting the official docs at https://docs.python.org/2.0/ref/strings.html:
So there is no difference. Instead, people will tell you to choose whichever style that matches the context, and to be consistent. And I would agree - adding that it is pointless to try to come up with "conventions" for this sort of thing because you'll only end up confusing any newcomers.
peko 11.2k4 gold badges32 silver badges47 bronze badges answered Sep 27, 2008 at 18:07
1 I used to prefer I have since changed to using triple quotes for
answered Sep 11, 2008 at 8:55
Daren ThomasDaren Thomas 66k40 gold badges149 silver badges198 bronze badges 12 I'm with Will:
I'll stick with that even if it means a lot of escaping. I get the most value out of single quoted identifiers standing out because of the quotes. The rest of the practices are there just to give those single quoted identifiers some standing room. answered Sep 11, 2008 at 10:21
Garth KiddGarth Kidd 7,0725 gold badges35 silver badges36 bronze badges If the
string you have contains one, then you should use the other. For example, If your code is going to be read by people who work with C/C++ (or if you switch between those languages and Python), then using The Python code I've seen in the wild tends to favour
Tadeck 128k28 gold badges149 silver badges197 bronze badges answered Sep 11, 2008 at 8:33
Tony MeyerTony Meyer 9,8476 gold badges44 silver badges47 bronze badges Triple quoted comments are an interesting subtopic of this question. PEP 257 specifies triple quotes for doc strings. I did a quick check using Google Code Search and found that triple double quotes in Python are about 10x as popular as triple single quotes -- 1.3M vs 131K occurrences in the code Google indexes. So in the multi line case your code is probably going to be more familiar to people if it uses triple double quotes. answered Jul 30, 2009 at 20:35
jblocksomjblocksom 13.6k5 gold badges34 silver badges30 bronze badges 0
For that simple reason, I always use double quotes on the outside. Always Speaking of fluff, what good is streamlining your string literals with ' if you're going to have to use escape characters to represent apostrophes? Does it offend coders to read novels? I can't imagine how painful high school English class was for you!
answered Sep 22, 2011 at 12:35
yurisichyurisich 6,8435 gold badges41 silver badges62 bronze badges 2 Python uses quotes something like this:
Which gives the following output:
Tadeck 128k28 gold badges149 silver badges197 bronze badges answered Jan 23, 2011 at 21:45
MichaelMichael 811 silver badge1 bronze badge 3 I use double quotes in general, but not for any specific reason - Probably just out of habit from Java. I guess you're also more likely to want apostrophes in an inline literal string than you are to want double quotes. answered Sep 11, 2008 at 8:40
Matt SheppardMatt Sheppard 115k46 gold badges108 silver badges129 bronze badges Personally I stick with one or the other. It doesn't matter. And providing your own meaning to either quote is just to confuse other people when you collaborate. answered Sep 28, 2008 at 3:35
schwaschwa 11.9k14 gold badges42 silver badges54 bronze badges It's probably a stylistic preference more than anything. I just checked PEP 8 and didn't see any mention of single versus double quotes. I prefer single quotes because its only one keystroke instead of two. That is, I don't have to mash the shift key to make single quote. answered Sep 19, 2008 at 19:34
MikeMike 3,5933 gold badges20 silver badges12 bronze badges 1
PHP makes the same distinction as Perl: content in single quotes will not be interpreted (not even \n will be converted), as opposed to double quotes which can contain variables to have their value printed out. Python does not, I'm afraid. Technically seen, there is no $ token (or the like) to separate a name/text from a variable in Python. Both features make Python more readable, less confusing, after all. Single and double quotes can be used interchangeably in Python. answered Jul 5, 2010 at 12:51
PeterinoPeterino 13.4k3 gold badges26 silver badges28 bronze badges 2 I chose to use double quotes because they are easier to see. answered Mar 10, 2009 at 6:25
Andrew DalkeAndrew Dalke 14.5k3 gold badges38 silver badges53 bronze badges I just use whatever strikes my fancy at the time; it's convenient to be able to switch between the two at a whim! Of course, when quoting quote characetrs, switching between the two might not be so whimsical after all... answered Jul 22, 2009 at 19:50 Your team's taste or your project's coding guidelines. If you are in a multilanguage environment, you might wish to encourage the use of the same type of quotes for strings that the other language uses, for instance. Else, I personally like best the look of ' answered Sep 11, 2008 at 8:25
Vinko Vrsalovic♦Vinko Vrsalovic 321k52 gold badges329 silver badges368 bronze badges None as far as I know. Although if you look at some code, " " is commonly used for strings of text (I guess ' is more common inside text than "), and ' ' appears in hashkeys and things like that. answered Sep 11, 2008 at 8:28
Mario FMario F 42.9k6 gold badges36 silver badges38 bronze badges I aim to minimize both pixels and surprise. I typically
prefer Perhaps it helps to think of the
pixel minimization philosophy in the following way. Would you rather that English characters looked like answered Apr 16, 2013 at 22:32
AsclepiusAsclepius 52.1k15 gold badges150 silver badges131 bronze badges I use double quotes because I have been doing so for years in most languages (C++, Java, VB…) except Bash, because I also use double quotes in normal text and because I'm using a (modified) non-English keyboard where both characters require the shift key. answered Jul 5, 2010 at 13:06
PhilippPhilipp 46.5k12 gold badges83 silver badges108 bronze badges
example :
Results are the same =>> no, they're not the same. A single backslash will escape characters. You just happen to luck out in that example because If you want to use single backslashes (and have them interpreted as such), then you need to use a "raw" string. You can do this by putting an '
As far as paths in Windows are concerned, forward slashes are interpreted the same way. Clearly the string itself is different though. I wouldn't guarantee that they're handled this way on an external device though.
Tadeck 128k28 gold badges149 silver badges197 bronze badges answered Jan 19, 2010 at 4:30
kn3lkn3l 19.1k28 gold badges87 silver badges123 bronze badges 1 Should I use single or double quotes?In US English, you must use double quotation marks. Single quotation marks are used for quotes within quotes. In UK English, it's most common to use single quotation marks, with double quotation marks for quotes within quotes, although the other way around is acceptable too.
Do quotation marks matter in Python?In Python, you can create a string with double quotation marks or single quotation marks. There is no difference. When using quotation marks inside a string, make sure to escape them not to cause syntax errors. A triple quotation mark in the string is used as a documentation string.
|