NOTICE: PERSONAL OPINION AHEAD (except this is my blog so of course it is personal opinion so why do I even bother to say that. :P)
In pretty much every programming language – I am unaware of one that does not work this way – both single and double quotes create string literals. There are occasions when an API may require a specific quote (the Blender Python API, for example, requires single quotes to work properly) but normally they are interchangeable. Thus which one you use comes down to personal preference.
As I said in the title, I prefer double quotes for my strings literals. In this post I will explain why I do this and some of the exceptions when I do not use them.
I am very good in English (it was one of my better subjects in homeschool), and one of the rules of quoting in English is to use double quotes then single quotes if the quote contains a quote, back to double if that contains a quote, and so on. An example of this would look like so:
In his work Person X wrote “After many years of painstaking work, I confirmed Person Y’s saying ‘I am almost positive Person Z was on the right track when he said “Roses are a perennial flower.” ‘ “
(I bet you were not expecting an English lesson with such a silly example, were you? :P)
I use double quotes all the time not solely because of this rule but because I see string literals almost like quotes so by English convention I would use double quotes, even in parameters (e.g.
<table border="0">). The exception to my convention is when
- I need a multi-line string
- I need to display double quotes in my string
- I need to create an HTML parameter
\ or string concentration with a
\n character), the other two are common exceptions I often encounter. So when these exceptions arise my string looks something like this:
'Could not open file "' + myFile + '" for writing'
That is why I use double quotes over single quotes in my code. Remember, this is simply personal preference. I am not trying to convince you of anything, simply stating an opinion. Agree, disagree? Let me know! I’m always open for discussion. 🙂