Python Single vs Double Quotes: Syntax and Style
python single quotes vs double quotes: Understand the technical differences between single and double quotes in Python, including escaping, triple quotes, f-strings, a...
python single quotes vs double quotes requires a clear understanding of the core syntax, runtime behavior, and practical implementation patterns demonstrated in the examples below.
In Python, single quotes (') and double quotes (") are interchangeable for defining string literals. The language does not treat them differently at runtime, but the choice affects how you write escape sequences and how readable your code becomes. This article explains the concrete differences and gives you practical rules for choosing between them.
The Core Difference: Escape Sequences
The only syntactical difference between single and double quotes is which character you must escape when it appears inside the string. A string delimited by single quotes requires you to escape any embedded single quote with a backslash. Similarly, a double-quoted string requires escaping embedded double quotes.
single_quoted = 'It\'s a string' double_quoted = "It's a string"
Both produce the same value, but the double-quoted version is more readable because it avoids the backslash. Conversely, if your string contains double quotes, single quotes are cleaner:
single_quoted = 'She said "hello"' double_quoted = "She said \"hello\""
Choose the delimiter that minimizes escaping. This is the primary practical rule for everyday string literals.
When Quotes Appear Inside Strings
Strings that contain apostrophes are common in natural language text. Using double quotes for such strings avoids escaping the apostrophe. Strings that contain quoted speech or attribute values in HTML or JSON often benefit from single quotes.
# Natural language with apostrophe message = "The user's session expired" # HTML attribute with double quotes html_fragment = '<a href="https://example.com">link</a>'
In the HTML example, single quotes allow the double quotes inside the string to remain unescaped. This keeps the string literal visually close to the actual markup. There is no functional advantage beyond readability, but readability directly affects maintainability.
Triple Quotes for Multiline Strings
Triple quotes (''' or """) are a separate syntax used for multiline strings and docstrings. They allow embedded newlines without using the newline escape character \n. The same escaping rule applies to the quote character that appears immediately after the opening delimiter, but you rarely need to escape a single quote inside a triple-double-quoted string.
triple_single = '''This string spans multiple lines and can contain "double quotes" easily.''' triple_double = """This string spans multiple lines and can contain 'single quotes' easily."""
PEP 8 recommends using triple double quotes (""") for docstrings to remain consistent with the standard library. For multiline strings that are not docstrings, either form works. Choose the one that avoids escaping the quote characters you need to embed.
F-Strings and Quote Choice
F-strings, introduced in Python 3.6, allow embedded expressions inside curly braces. The quote choice for the f-string itself follows the same rules as regular strings, but you must also consider the quotes used inside the expression.
name = "Ada" # Double quotes outside, single quotes inside the expression message = f"{name} said 'hello'" # Single quotes outside, double quotes inside the expression message2 = f'{name} said "hello"'
If the expression itself contains a string literal, you need to use the opposite quote type to avoid escaping. For example, when accessing a dictionary key that is a string:
data = {"status": "active"} # Double quotes for the f-string, single quotes for the key result = f"Status: {data['status']}"
This is a common source of confusion. The rule is the same as for regular strings: pick the outer delimiter that lets you write the inner content without backslashes. In f-strings, this becomes more important because backslashes inside the expression part are not allowed in older Python versions (before 3.12), so avoiding them is necessary.
Style Guide Recommendations
PEP 8, the official style guide for Python code, does not mandate a specific quote type. It states that single quotes and double quotes are equivalent, and you should pick one and use it consistently. The guide specifically says: "Pick a rule and stick to it." When a string contains the other quote character, use the opposite delimiter to avoid backslashes.
Most Python projects follow this guidance. The standard library tends to use single quotes for short strings, but many popular frameworks and codebases prefer double quotes. The key is consistency within a file or project. If you are contributing to an existing codebase, follow its established convention. If you are starting a new project, decide on one style and apply it uniformly.
Does Quote Choice Affect Performance?
No. At the bytecode level, single-quoted and double-quoted strings compile to the same object. The Python compiler treats them identically; the quote character is only a lexical marker. There is no runtime performance difference, no memory difference, and no caching difference.
import dis def single(): return 'hello' def double(): return "hello" dis.dis(single) dis.dis(double)
Both functions produce identical bytecode. Any performance claim about one quote style being faster is unfounded. Focus on readability and consistency instead.
Practical Decision Rules
When writing a string literal, apply these rules in order:
- If the string contains a single quote and no double quotes, use double quotes to avoid escaping.
- If the string contains a double quote and no single quotes, use single quotes to avoid escaping.
- If the string contains both, use the delimiter that produces the fewest escaped characters, or use triple quotes if the string is multiline.
- For docstrings, use triple double quotes to match PEP 8 and the standard library.
- For f-strings, choose the outer delimiter based on the quotes needed inside the expression, and avoid backslashes in the expression part.
- Be consistent within a project. If you inherit a codebase with a convention, follow it.
These rules cover the vast majority of real-world cases. The choice between single and double quotes is a stylistic decision with a concrete technical impact only when escaping is involved. Understanding that impact lets you write cleaner, more maintainable Python code.