Add Double Quotes to a String in Python

Recently, while working on a project that involved formatting text for a JSON file, I needed to add double quotes around strings in Python. It sounds simple, right? But depending on how you’re handling strings, it can be tricky to get the quotes to appear exactly where you want them.

Over my years as a Python developer, I’ve come across multiple ways to do this. Some are quick one-liners, while others are better suited for dynamic or large-scale string manipulation.

In this tutorial, I’ll walk you through five easy methods to add double quotes to a string in Python. We’ll look at examples, discuss when to use each method, and I’ll share some tips I’ve learned from real-world coding experience.

Method 1 – Use Escape Characters in Python

One of the simplest ways to add double quotes to a string in Python is by using escape characters (\"). This method works great when you want to include quotes directly inside your string literal.

Here’s how I usually do it:

text = "Python"
quoted_text = "\"" + text + "\""
print(quoted_text)

When you run this code, the output will be:

"Python"

You can see the output in the screenshot below.

python add quotes to string

In this example, I used the backslash (\) before the double quote to tell Python that it’s part of the string, not the end of it. This is one of the most common ways I use when formatting strings for display or logging.

You can also use this technique inside an f-string, which makes it even cleaner.

text = "Data Science"
quoted_text = f"\"{text}\""
print(quoted_text)

Output:

"Data Science"

This approach is simple, effective, and works perfectly when you’re dealing with smaller strings or static content.

Method 2 – Use Single Quotes Around the Python String

Another easy way to add double quotes to a string in Python is by wrapping your string in single quotes. This way, you can include double quotes inside without escaping them.

Here’s an example:

text = "AI Engineer"
quoted_text = '"' + text + '"'
print(quoted_text)

Output:

"AI Engineer"

You can see the output in the screenshot below.

add double quotes to string python

This method is often my go-to when I’m writing quick scripts or working with static text. It’s clean and readable because you don’t need to worry about escape characters.

You can also use this approach directly in formatted strings:

# Example 4: Using single quotes in formatted strings
text = "Python Developer"
quoted_text = f'"{text}"'
print(quoted_text)

Output:

"Python Developer"

This method is great for readability, especially when you’re teaching beginners or working on scripts that need to be easily maintained.

Method 3 – Use the repr() Function in Python

The repr() function in Python returns a string that includes quotes, it’s often used for debugging or representing objects as strings.

Here’s how it works:

text = "Machine Learning"
quoted_text = repr(text)
print(quoted_text)

Output:

'Machine Learning'

You can see the output in the screenshot below.

add quotes to string python

Notice that the output uses single quotes. If you specifically want double quotes, you can replace them like this:

text = "Machine Learning"
quoted_text = repr(text).replace("'", '"')
print(quoted_text)

Output:

"Machine Learning"

I often use this trick when generating JSON-like data or preparing strings for APIs that require double-quoted keys or values. It’s a neat and Pythonic approach.

Method 4 – Use Python’s format() Function

The format() method is another flexible way to add double quotes around a string in Python. It’s especially useful when you’re formatting multiple strings dynamically.

Here’s a simple example:

text = "Data Analytics"
quoted_text = '"{}"'.format(text)
print(quoted_text)

Output:

"Data Analytics"

You can see the output in the screenshot below.

how to add quotation marks in python string

This method is very readable and works well when you’re generating formatted text for reports, logs, or structured documents.

You can even combine it with loops to add quotes to multiple strings at once:

words = ["Python", "Flask", "Django"]
quoted_words = ['"{}"'.format(word) for word in words]
print(quoted_words)

Output:

['"Python"', '"Flask"', '"Django"']

This is one of my favorite methods when I need to format lists of strings for JSON or CSV files.

Method 5 – Use the join() Function in Python

If you’re working with multiple strings and want to add quotes around each one, the join() function can make your code neat and efficient.

Here’s how I do it:

words = ["New York", "Los Angeles", "Chicago"]
quoted_words = ['"{}"'.format(word) for word in words]
result = ", ".join(quoted_words)
print(result)

Output:

"New York", "Los Angeles", "Chicago"

This method is perfect when you’re preparing CSV-style data or generating formatted strings for APIs. It’s both clean and efficient, two things I always aim for in Python code.

Bonus Tip – Add Double Quotes in a List of Strings

Sometimes you might have a list of strings and want to add quotes to each element. Here’s how you can do that in one line using a list comprehension:

cities = ["Boston", "Houston", "Seattle"]
quoted_cities = [f'"{city}"' for city in cities]
print(quoted_cities)

Output:

['"Boston"', '"Houston"', '"Seattle"']

This is a clean and Pythonic way to handle string formatting in bulk. I use this often when I’m exporting data to CSV or JSON files.

When to Use Each Method

Here’s a quick summary of when to use each method:

MethodBest ForExample Use Case
Escape CharactersSimple inline textLogging or debugging
Single QuotesReadable string formattingDisplay or printing
repr()Debugging or JSON-like outputAPI responses
format()Dynamic string formattingReports or templates
join()Multiple stringsCSV or batch formatting

Each method has its own strengths. Personally, I prefer f-strings or format() for readability, and join() when working with lists of strings.

Common Mistakes to Avoid

When adding double quotes in Python strings, here are a few mistakes I’ve seen beginners make:

  1. Forgetting to escape quotes – This causes syntax errors.
  2. Using wrong quote types – Mixing single and double quotes incorrectly.
  3. Adding redundant quotes – Ending up with triple quotes accidentally.
  4. Using repr() without cleaning up – It adds single quotes by default.

Always check your output carefully, especially if you’re generating data for APIs or file exports.

Adding double quotes to a string in Python is a simple but essential skill, especially when you’re working with text formatting, data serialization, or user-facing outputs.

Over the years, I’ve found that the best method depends on the context. For quick formatting, escape characters or f-strings are perfect. For structured data, format() and join() give you more control.

You may like to read:

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.