How to Use Single and Double Quotes in Python

A small reporting script often starts with simple labels such as customer names, status messages, and file paths. Then someone adds a name like O'Connor or a message with double quotes, and Python suddenly throws a syntax error.

I have hit this problem many times while building automation scripts that read CSV files, create reports, and write log messages. The fix is simple once you understand how Python treats single quotes and double quotes.

This practical Python tutorial shows when to use each quote style, how to handle quotes inside strings, and how to avoid common mistakes.

What Are Single and Double Quotes in Python?

In Python, a string is text data. It can contain letters, numbers, symbols, spaces, file paths, JSON data, messages, and more.

You create a string by placing text inside either single quotes (') or double quotes (").

report_name = 'Monthly Sales Report'
status = "Completed"

Both variables hold strings, and Python treats them the same way.

print(report_name)
print(status)

Output:

Monthly Sales Report
Completed

You can refer to the screenshot below to see the output.

Use Single and Double Quotes in Python

Python does not assign a different data type based on quote style. Both values use the str data type.

print(type('Python'))
print(type("Python"))

Output:

<class 'str'>
<class 'str'>

For most Python scripts, you can choose either style. The important part is consistency and choosing the quote that makes the string easier to read.

If you are new to variables, review this guide on Python variables before moving into larger string-handling scripts.

How to Use Single and Double Quotes in Python

You can use single or double quotes for ordinary text. Python 3.10 or later works perfectly for every example in this article.

Use Single Quotes for Simple Strings

Single quotes work well for short labels, keys, filenames, and simple text values.

file_name = 'sales_report.csv'
department = 'Finance'
message = 'Report created successfully'

print(file_name)
print(department)
print(message)

This code stores three text values for a small reporting script. I often use single quotes for short internal values because they keep the code compact and readable.

You may also use single quotes for dictionary keys.

report = {
'name': 'Monthly Sales',
'status': 'Ready',
'rows_processed': 250
}

print(report['status'])

Output:

Ready

The dictionary uses strings as keys and values. If you work with structured data, learning how to create a dictionary in Python using a for loop helps when your script builds records dynamically.

Use Double Quotes When Text Contains an Apostrophe

Use double quotes when your string includes an apostrophe. An apostrophe is also a single quote character, so single quotes would end the string too early.

For example, this code fails:

customer_name = 'O'Connor'

Python reads 'O' as the complete string. It then sees Connor' as invalid code.

Use double quotes instead:

customer_name = "O'Connor"
message = "Today's report is ready."

print(customer_name)
print(message)

Output:

O'Connor
Today's report is ready.

You can refer to the screenshot below to see the output.

Single and Double Quotes in Python

Double quotes make the code easier to read because you do not need extra escape characters.

This approach is especially useful in automation scripts that process customer names, comments, addresses, or natural-language messages.

Use Single Quotes When Text Contains Double Quotes

The same idea works in reverse. Use single quotes when your string contains double quotes.

log_message = 'The job returned "Success".'
print(log_message)

Output:

The job returned "Success".

This style works well when you create output that includes quoted values.

For example, a script that checks a CSV upload might create a log message like this:

uploaded_file = 'orders.csv'
message = 'File "orders.csv" passed validation.'

print(message)

Using the opposite quote style avoids clutter and makes the message easy to scan during debugging.

Escape Quotes Inside Python Strings

Sometimes your string needs both quote types. In that situation, use an escape character.

An escape character tells Python to treat the next character as regular text instead of special syntax. In Python, the backslash (\) acts as the escape character.

Escape a Single Quote

Use \' when you need a single quote inside a string that starts and ends with single quotes.

message = 'Don\'t overwrite the source file.'
print(message)

Output:

Don't overwrite the source file.

Python does not treat the escaped apostrophe as the end of the string.

Escape a Double Quote

Use \" when you need double quotes inside a string that starts and ends with double quotes.

message = "The status is \"Completed\"."
print(message)

Output:

The status is "Completed".

This approach works, but I only use it when I need the same quote style around the whole string. If switching quote styles removes the backslashes, I prefer switching styles.

For example, this version is cleaner:

message = 'The status is "Completed".'
print(message)

Pro Tip: In my experience, switching quote styles is usually better than adding escape characters. It makes log messages, email text, and API payloads much easier to maintain.

Single and Double Quotes in Python Strings

For simple Python strings, single and double quotes give the same result. The choice becomes important when your text contains quotes or when your team follows a code style.

Here is a practical comparison:

SituationBetter ChoiceExample
Simple internal labelEither quote style'Pending'
Text with an apostropheDouble quotes"Today's total"
Text with double quotesSingle quotes'Status: "Ready"'
String needs both quote typesEscape one type"She said, \"Don't stop.\""
Multi-line textTriple quotes"""Line one"""

Python developers often choose one style as a default. Many teams use single quotes for normal strings and double quotes when the text includes apostrophes. Other teams choose double quotes everywhere.

Neither choice changes script performance. Choose a clear convention and apply it consistently across files.

For more ways to work with text values, see how to print strings and variables in Python.

Use Triple Quotes for Multi-Line Strings

Python also supports triple quotes. Use three single quotes (''') or three double quotes (""") to create a multi-line string.

This feature helps when your reporting script creates an email body, SQL query, HTML template, or detailed log entry.

email_body = """
Hello Team,

The daily sales report is ready.
Please review the attached CSV file.

Thanks,
Automation Bot
"""

print(email_body)

Python keeps the line breaks inside the string. That makes triple quotes useful for text that spans several lines.

You can also use triple single quotes.

instructions = '''
1. Download the report.
2. Review failed rows.
3. Send the final file.
'''

print(instructions)

Both approaches work. I typically use triple double quotes for multi-line messages because they stand out clearly in Python files.

If you need multi-line strings inside formatted output, learn how to use Python triple quotes with f-strings.

Use Quotes with f-Strings

An f-string lets you insert variable values directly into a string. It is one of the cleanest ways to build messages in modern Python.

Add the letter f before the opening quote, then place variables inside curly braces.

customer = "O'Connor"
total = 1250.50

message = f"Customer: {customer}, total: ${total:.2f}"
print(message)

Output:

Customer: O'Connor, total: $1250.50

You can refer to the screenshot below to see the output.

How to Use Single and Double Quotes in Python

This reporting example uses double quotes because the customer name contains an apostrophe. The :.2f format code displays the number with two decimal places.

You can also use single quotes with f-strings.

report_date = '2026-08-04'
message = f'Report generated on {report_date}.'

print(message)

When an f-string includes dictionary values, choose quotes carefully.

report = {'name': 'Sales Summary', 'status': 'Ready'}

message = f"Report: {report['name']} | Status: {report['status']}"
print(message)

The outer f-string uses double quotes, while the dictionary keys use single quotes. This combination prevents quote conflicts.

Quotes in File Paths and Raw Strings

Windows file paths often include backslashes. Python treats some backslash combinations as escape sequences, so paths can create unexpected results.

For example:

file_path = "C:\new_reports\sales.csv"
print(file_path)

Python may treat \n as a new line. Use a raw string by adding r before the opening quote.

file_path = r"C:\new_reports\sales.csv"
print(file_path)

Output:

C:\new_reports\sales.csv

A raw string tells Python to keep backslashes as regular characters. You can use either quote style with raw strings.

backup_path = r'C:\reports\backup\sales.csv'
print(backup_path)

Raw strings help when scripts read files locally, move documents, or process server folders. For a deeper explanation, read this guide on raw strings in Python.

Things to Keep in Mind

  • Stay consistent: Pick a default quote style for each project so teammates can read the code quickly.
  • Match the quote content: Use double quotes for apostrophes and single quotes for embedded double quotes when possible.
  • Escape only when needed: Use \' or \" when your string requires both quote types.
  • Watch file paths: Use raw strings for Windows paths so Python does not interpret backslashes as escape sequences.
  • Check unmatched quotes: A missing closing quote causes an unterminated string literal error and stops the script before it runs.
  • Use f-strings carefully: Match outer and inner quote styles when you access dictionary keys inside an f-string.

Frequently Asked Questions

Should I use single or double quotes in Python?

Use either one for ordinary strings because Python treats them the same. Choose the style that avoids escaping characters and matches your project’s existing code.

Are single quotes and double quotes the same in Python?

Yes, both create a Python str object. They differ only in how easily they handle quote characters inside the text.

Why does Python show an unterminated string literal error?

Python shows this error when it cannot find the matching closing quote. Check for a missing quote, an unescaped quote inside the string, or text split across lines without triple quotes.

How do I put an apostrophe in a Python string?

Use double quotes around the string, such as "Don't stop". You can also use single quotes and escape the apostrophe: 'Don\'t stop'.

Can I use single quotes in an f-string?

Yes. Write an f-string as f'Hello, {name}' or f"Hello, {name}". Choose the outer quote style that does not conflict with quotes inside expressions.

When should I use triple quotes in Python?

Use triple quotes for strings that span multiple lines, such as email templates, SQL queries, instructions, and documentation text. They also help when your message contains both single and double quotes.

Single and double quotes in Python both create strings, but the right choice keeps your code clean and prevents avoidable syntax errors. Start with one consistent style, switch quote types when the text includes quotes, and use escapes only when necessary.

You May Also Like

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.