Strings are enclosed in quotes because that's how a programming language knows where text begins and ends. Without quote marks, a compiler or interpreter can't tell whether hello is a piece of text to print or the name of a variable. The difference between single and double quotes depends on the language: in JavaScript and PHP, they behave differently for variable interpolation; in Python and Java, single and double quotes are interchangeable for simple strings; in C and C++, only double quotes make strings while single quotes make single characters.
That's the whole answer in one breath. The rest of this article walks through the examples, the failure cases, and the edge rules you'll bump into the moment you write real code. Think of it as one programmer explaining across a table, not a lecture.
Why Strings Need Quote Marks at All
A program is full of names. Variables, functions, keywords, and operators all sit in the same text file, and the language needs a reliable way to separate them from text a human wants to read. Quotes are that signal. When the parser sees "Hello", it stops treating what follows as an identifier and starts storing characters.
Try to imagine code without quotes. You write print(hello) and the language has to guess: is hello a variable, a function, or the literal word? Most languages treat it as a variable and throw an error if no such thing exists. The quote marks resolve the ambiguity by saying, "This is data, not a name." According to the Python documentation on lexical analysis, string literals are defined precisely by which quote character opens them, which is why mismatched quotes break parsing rather than being silently fixed.
Quotes also carry escaping rules. A backslash inside a string tells the compiler the next character is special β \n for a newline, \t for a tab, " for a literal quote. Those rules only exist because the language knows it's inside string context. Without delimiters, escape sequences would be meaningless.
The cost of getting quotes wrong is subtle. A missing closing quote doesn't just fail one line; in many languages it swallows the rest of the file into a single unterminated string and echoes a misleading error message far below the real problem. Learning to spot quote mismatch first saves hours of debugging.
Single vs Double Quotes: The Plain-English Rule by Language
There is no universal rule. Each language sets its own convention, and the table below collects the ones you'll hit most often. The "Behavior" column describes whether the two quote types are interchangeable for a simple string like hello.
| Language | Single vs double behavior | Interpolation in double quotes | Interpolation in single quotes | Notes |
|---|---|---|---|---|
| Python | Interchangeable | Yes (with f-strings only) | Yes, in f-strings | Style guide prefers double for docstrings |
| JavaScript | Different | Yes (${var} in template literals) | No | Single quotes are conventional in most JS style guides |
| PHP | Different | Yes ($var) | No (except escaped $) | Double quotes process \n; single quotes keep it literal |
| Java | Interchangeable for String | N/A (+ concatenation) | N/A | Single quotes are reserved for char |
| C / C++ | Different | N/A | N/A | Double = string, single = char |
| Ruby | Different | Yes | No | Single quotes only escape \ and ' |
| C# | Interchangeable for string | No (use $"...") | No | Single quotes are for char only |
| Go | Interchangeable for cold strings | No | No | Backticks are raw strings |
| Bash | Different | Yes ($var) | No | Single quotes are truly literal |
Read that table slowly if you're new. The surprising entries for most beginners are PHP and Bash, where single and double quotes behave very differently at runtime, and C/C++, where single quotes aren't a string at all. Those four cases cause the vast majority of "why is my string broken" questions on forums.
What Interpolation Actually Means in Practice
Interpolation is the act of dropping a variable's value into a string without concatenating. In JavaScript, "Hello, " + name and Hello, ${name} produce the same output, but template literals (backticks) are the interpolation form and single/double quotes are not. In PHP, "Hello, $name" interpolates while 'Hello, $name' prints the literal dollar sign and variable name.
Here's a small worked example. In PHP:
$name = "Sam"; echo "Hi, $name"; prints Hi, Sam echo 'Hi, $name'; prints Hi, $name
Same string content, different output because the parser treats the dollar sign differently inside single quotes. The PHP manual's string section documents this explicitly under "Single quoted" and "Double quoted" subsections, and it's the reason many PHP style guides recommend single quotes for strings with no variables β they're marginally faster because the parser skips interpolation checks.
JavaScript has a similar but less dangerous difference. Single and double quotes are functionally identical for simple strings; only backticks interpolate. Ruby, by contrast, makes double quotes interpolate and single quotes literal β so "Hello, #{name}" gives you Sam while 'Hello, #{name}' gives you the braces.
The takeaway: interpolation is the single biggest practical difference between quote types, and it exists only in some languages.
Escape Sequences: Where the Two Quote Types Diverge Again
Escape sequences are backslash-prefixed characters that represent something you can't type directly. \n is newline, \t is tab, \ is a literal backslash, and ' or " let you include a quote inside a string.
The behavior splits by quote type in a pattern that trips up beginners. In PHP double quotes, \n becomes a newline. In single quotes, it stays as a backslash and the letter n, printed literally. In Ruby, the same is true β single quotes only recognize \ and '. In Python, both quote types process escapes identically. In Bash, double quotes process $, backtick, , and !, while single quotes process nothing.
Consider a Windows file path: C:\new\table.txt. If you put that in a PHP double-quoted string, \n becomes a newline and \t becomes a tab, mangling the path. Wrapping it in single quotes preserves the backslashes exactly. That one failure case accounts for a huge share of confused file-not-found errors in beginner PHP code.
If you remember nothing else, remember this: single-quoted strings in PHP, Ruby, and Bash behave like literal text. Double-quoted strings in those languages behave like a mini-template. When in doubt, use single quotes for paths, regex patterns, and any text containing $ or backslash.
The Language Trap: C, C++, Java, and C# Treat Single Quotes Differently
In C, C++, Java, and C#, a single quote does not make a string. It makes a single character. 'A' is one character. "A" is a string of one character followed by a null terminator (in C) or a String object (in Java and C#).
This distinction matters because the two types aren't interchangeable. Passing 'A' where a string is expected produces a compile error at best and a garbage value at worst. Old C compilers, for example, treat 'ab' as a multi-character integer literal whose value depends on byte order β a well-known portability hazard documented in the C standard's section on character constants. If you're writing cross-platform code, never put more than one character inside single quotes.
Java goes further. Single quotes wrap char (a 16-bit Unicode code unit) and double quotes wrap String. Concatenating a char to a String works, but it promotes the character, so "a" + 'b' + 'c' becomes "abc" while 'a' + 'b' + 'c' becomes the integer 294, because char addition happens before any string conversion. That's a classic interview question and a real bug in careless code.
For anyone coming from Python or JavaScript, where quotes are roughly interchangeable, the C-family rule feels arbitrary. It isn't β it's a legacy of C's design, where characters and strings are genuinely different memory layouts. Learn the rule once and it sticks.
Edge Cases That Break Code: Quotes Inside Strings and Across Lines
Putting a quote inside a string sounds trivial and is where most beginners get stuck. If your string uses double quotes on the outside and you need a double quote inside, you escape it: "She said "hi"". If you use single quotes outside and need an apostrophe inside, escape it: 'It's fine'. Mixing outer types avoids escaping altogether β "It's fine" needs nothing special because the outer quotes are double.
Newlines are another landmine. In most languages a plain string cannot span multiple lines unless you use a specific syntax. JavaScript template literals (backticks) allow real newlines. Python triple quotes ("""...""" or '''...''') allow them. PHP double-quoted strings allow line breaks but preserve physical newlines in the file. C and Java string literals cannot span lines at all; you concatenate or use +.
A failure case worth knowing: in JavaScript, a stray apostrophe in single-quoted text ('It's here') ends the string at It and leaves s here' as syntax garbage. The error message usually points to the wrong line. If you paste human text with apostrophes into single-quoted JS, either switch to double quotes or escape apostrophes. It's a two-second fix that trips up new coders for ten minutes because the parser error is misleading.
Word Processors and "Smart Quotes": A Silent Failure
Open a code file that was edited in Microsoft Word and you may see curly quotes like β and β instead of straight quotes. Those characters are unicode code points U+201C and U+201D, not the ASCII double quote U+0022. Compilers almost universally reject them, but some editors display them so similarly that you can't see the problem.
This is a genuine edge scenario that catches people who paste code from a Word document, email, or web article into their editor. The fix is to retype the quote in the editor or run a search-and-replace from curly to straight. Many editors (VS Code, for instance) don't auto-correct this, so the bug lives quietly until the next compile.
Stack Overflow's own canonical question on the topic β "My code doesn't compile, and the quotes look fine" β is littered with cases solved by this one observation. If a compile error points to a quote that looks correct, zoom in on the character or copy the file into a plain-text editor to check it visually.
Quotes and SQL: A Different Rule Entirely
SQL adds a layer most tutorials skip. In standard SQL, single quotes delimit string literals and double quotes delimit identifiers (table names, column names). That's reversed from most programming languages, which is why someone coming from JavaScript expects double quotes and hits an error.
In MySQL, a 'string' is a value and a "string" can be either a value or an identifier depending on the ANSI_QUOTES SQL mode setting. PostgreSQL is stricter: '...' is always a string and "..." is always an identifier, so SELECT "name" FROM users retrieves the column called name, not the text "name". According to PostgreSQL's SQL syntax documentation, this is intentional and matches the SQL standard, while MySQL's flexibility is a known compatibility trap.
SQL injection risks also hang off this rule. A naive string concatenation like WHERE name = '" + userInput + "' breaks if the input contains a single quote. Parameterized queries are the correct answer, but understanding that the single quote is a string delimiter is the first defense. Get the quote rules right and the injection class becomes easier to reason about.
A Practical Checklist for Choosing Quotes
Here's the decision rule most experienced developers follow. It's not a law, just a habit that reduces bugs.
1: In JavaScript, TypeScript, C#, and Go, use single quotes for simple strings and backticks or template literals when interpolation is needed. 2: In PHP, use single quotes unless interpolation is needed; then switch to double. 3: In Ruby and Bash, use single quotes for literal text and double only for interpolation. 4: In Python, follow your team's style guide; PEP 8 doesn't mandate one. Google's Python style guide prefers double quotes. 5: In SQL, always single-quote string literals and always double-quote identifiers, unless your engine's mode says otherwise.
If you write across multiple languages, the rule is simpler: default to single quotes for literal strings unless your language interpolates only in double, then pick the interpolation form the moment you need a variable. The table above shows where that line sits for each language.
That checklist isn't about correctness in a vacuum. It's about avoiding the escape sequences and interpolation mistakes that slow debugging. Consistency across a codebase β linters like ESLint, Prettier, and Ruff can enforce it automatically β matters more than the choice itself. Pick a rule, apply it, and move on.
FAQ
Why do some languages require double quotes but not single quotes for strings?
In C and C++, double quotes are the only way to declare a string because the language distinguishes strings (arrays of characters) from single characters (the char type). The C standard's lexical rules define string literals with double quotes explicitly, and the same convention carried into descendants like Java and C# for strings, even though both also accept single-character literals. The rule is a historical inheritance, not a universal one.
What happens if I use smart quotes from Microsoft Word in code?
Smart quotes are Unicode characters U+201C and U+201D, not the ASCII quote that compilers recognize, so the file almost always fails to compile with an error pointing at the wrong line. Editors like VS Code, Sublime, and Notepad++ may not flag the character visually. Copy the file into a plain-text editor or run a replace from curly to straight quotes to spot and fix it.
Can a string contain a real newline in JavaScript?
Only if the string is written with backticks (a template literal) or uses the \n escape sequence. Single and double-quoted strings in JavaScript cannot span physical lines β the parser throws a SyntaxError at the first line break. This is why multiline text in older codebases is concatenated with + or built as array joins.
Why does 'a' + 'b' return a number in Java?
In Java, characters are numeric types under the hood. Adding two char values before either is concatenated to a String performs integer addition, so 'a' (97) plus 'b' (98) equals 195, not "ab". Concatenating a String first forces the character into a string, which is why "" + 'a' + 'b' gives "ab". This is documented in Java's language specification on additive operators.
Is there a performance difference between single and double quotes?
In PHP and Ruby, double-quoted strings are slightly slower to parse because the engine scans for interpolation and escape sequences. Benchmarks from PHP's own community documentation note that the difference is on the order of microseconds for most strings, which is irrelevant for typical web request handling. Single quotes are preferred in PHP style guides mostly for clarity and consistency, not speed.
How do I include a quote character inside a string?
Use the opposite quote type for the outer delimiter, or escape the inner quote with a backslash. In JavaScript, "It's fine" works without escaping because the outer quote is double; 'It's fine' also works because the apostrophe is escaped. In SQL, two adjacent single quotes ('') represent one literal single quote inside a single-quoted string, not a backslash escape.
Why does my SQL query fail when a value contains an apostrophe?
Because a single quote is SQL's string delimiter, an unescaped apostrophe ends the string early and leaves the rest as garbage syntax. The standard way to include an apostrophe is doubling it ('It''s fine') or, better, using parameterized queries where the driver handles the escaping. Never concatenate user input into SQL strings, as it's the primary path for SQL injection.
Do Python's single and double quotes ever behave differently?
For plain strings, no. For f-strings, yes in one obscure way: prior to Python 3.12, the expression inside an f-string could not use a quote character that matched the outer delimiter, so f"{"key"}" was a syntax error. Python 3.12 lifted that restriction, and older style guides recommended double-quoted f-strings with single-quoted inner strings to avoid the issue.