Python
programming
syntax
quotes
string-handling

Single quotes vs. double quotes in Python

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Python, single quotes and double quotes both create string literals with the same runtime type. Most of the time there is no performance or semantic difference. The practical differences are about readability, escaping, and team style consistency.

Equivalent String Semantics

These declarations produce identical values.

python
1a = 'hello'
2b = "hello"
3
4print(a == b)
5print(type(a), type(b))

Both lines create str objects. Python does not treat one quote style as more correct.

Choosing Quotes to Minimize Escaping

Pick the outer quote style that avoids extra backslashes.

python
1text1 = "It's easier to read this with double quotes."
2text2 = 'He said "ship it" during the review.'
3
4print(text1)
5print(text2)

This rule keeps literals cleaner and reduces accidental escape mistakes.

Triple Quotes for Multi-Line Content

For multi-line strings, use triple quotes with either style.

python
1query = """
2SELECT id, name
3FROM users
4WHERE active = 1
5"""
6
7message = '''
8Line one
9Line two
10Line three
11'''
12
13print(query.strip())
14print(message.strip())

Triple quoted strings are convenient for templates, SQL snippets, and structured messages.

Interaction With f-Strings and Raw Strings

Quote style stays flexible with prefixes.

python
1name = "Ana"
2path = r"C:\projects\demo"
3status = f"User '{name}' is active"
4
5print(path)
6print(status)

Use raw strings for patterns and paths where many backslashes appear. Use f-strings when interpolation improves clarity.

Bytes, Unicode, and Escapes

The quote decision does not change Unicode handling, but it affects readability around escape sequences.

python
1snowman = "\u2603"
2byte_value = b"ABC"
3
4print(snowman)
5print(byte_value)

Remember that b"..." creates bytes, not text strings. If you decode bytes later, quote style still remains a formatting choice, not a behavior change.

Formatter and Linter Behavior

Most teams rely on auto-formatters. If your project uses Black, it will usually normalize quotes to double style unless that would add more escapes. The best practice is to follow the formatter output and avoid manual restyling.

This also reduces merge conflicts. Two developers editing nearby lines are less likely to conflict when quote rules are machine-enforced.

Style Guidance in Real Projects

Many teams choose one default quote style to reduce noisy diffs. For example, black often normalizes to double quotes unless escaping would increase.

A practical policy:

  • use the formatter default
  • switch quote type when it reduces escapes
  • avoid manual churn that only changes quote style

This keeps review discussions focused on behavior rather than formatting.

Docstrings Are a Special Case

PEP 257 recommends triple double quotes for docstrings.

python
def total(items):
    """Return the sum of numeric items."""
    return sum(items)

Using a consistent docstring style improves generated docs and tooling support.

Another practical tip is to keep user-facing text literals readable first, then let tooling enforce consistency. If escaping starts to dominate a literal, change outer quotes instead of adding more backslashes.

Common Pitfalls

  • Mixing quote styles randomly in the same file, which hurts readability.
  • Over-escaping strings when a different outer quote would be simpler.
  • Confusing apostrophes in natural language text with closing delimiters.
  • Editing large files only to flip quotes, creating noisy version control history.
  • Assuming quote style affects runtime speed in typical Python code paths.

Summary

  • Single and double quotes are functionally equivalent for Python strings.
  • Choose the style that minimizes escapes in each literal.
  • Use triple quotes for multi-line text and docstrings.
  • Let formatters enforce consistency to reduce style debates.
  • Optimize for readability and maintainable diffs, not perceived micro-performance.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.