Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python Single vs Double Quotes: Syntax and Style

In Python, single and double quotes are interchangeable for string literals. This guide covers escaping, triple quotes, f-strings, and PEP 8 style recommendations.

~4 min readPython
#python#string literals#syntax#PEP 8
Python code editor showing a string literal with both single and double quotes, illustrating the difference in escaping.

Python does not treat single quotes and double quotes differently at runtime; they are interchangeable for defining string literals. The practical difference is escaping: a single-quoted string must escape embedded single quotes, and a double-quoted string must escape embedded double quotes. This article explains that rule, shows examples where each style is clearer, and covers triple quotes, f-strings, and PEP 8 style guidance.

The Core Difference: Escape Sequences

The only syntactic 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.

python
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:

python
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, HTML attributes, or JSON payloads often benefit from single quotes, because those formats use double quotes.

python
# Natural language with apostrophe message = "The user's session expired" # HTML attribute with double quotes html_fragment = '<a href="https://example.com">link</a>' # JSON payload with double quotes json_fragment = '{"status": "active"}'

In the HTML and JSON examples, single quotes allow the double quotes inside the string to remain unescaped. This keeps the string literal visually close to the actual text. 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. Because the delimiter is three quote characters, you can usually include ordinary single or double quotation marks inside without escaping. The rare exception is a sequence of three consecutive quote characters that match the delimiter.

python
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.

python
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:

python
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 is especially important on Python versions before 3.12, where backslashes are not allowed inside the expression portion.

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. The quote character is a lexical marker; it is not part of the string value and does not change how Python stores or evaluates the literal. At the bytecode level, identical single-quoted and double-quoted strings are treated the same.

python
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:

  1. If the string contains a single quote and no double quotes, use double quotes to avoid escaping.
  2. If the string contains a double quote and no single quotes, use single quotes to avoid escaping.
  3. If the string contains both, use the delimiter that produces the fewest escaped characters, or use triple quotes if the string is multiline.
  4. For docstrings, use triple double quotes to match PEP 8 and the standard library.
  5. For f-strings, choose the outer delimiter based on the quotes needed inside the expression, and avoid backslashes in the expression portion if you support Python versions before 3.12.
  6. 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.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.