How to Put Quotes Inside a String in Python
A Python string can be written in single quotes, double quotes or triple quotes. To put one kind of quote inside a string, wrap the string in the other kind, put a backslash before the quote, or use triple quotes.
Syntax
'<text with "double quotes">'
"<text with 'single quotes'>"
"<text with \"escaped\" double quotes>"
"""<text with 'both' "kinds">"""
| Part | What it does |
|---|---|
'...' | A string in single quotes. Double quotes inside it are ordinary characters. |
"..." | A string in double quotes. Single quotes inside it are ordinary characters. |
\" or \' | A backslash makes the next quote part of the text instead of the end of the string. |
"""...""" | A triple-quoted string. Single and double quotes inside it are ordinary characters. |
The Python language reference on string literals: "String literals are text enclosed in single quotes (') or double quotes (")."
The Example
The example writes a piece of JSON, which is full of double quotes, inside single quotes. It escapes the double quotes of a second string with backslashes, and writes a third in triple quotes so it can hold an apostrophe and double quotes at once.
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.
Step 1: Write the Code
Create a file named main.py:

json_text = '{"name": "Ada", "role": "admin"}'
escaped = "She said \"hi\" and left."
both = """It's "fine"."""
print(json_text)
print(escaped)
print(both)
Step 2: Run It
uv run python main.py
{"name": "Ada", "role": "admin"}
She said "hi" and left.
It's "fine".

The command is the same in zsh, bash and PowerShell.
All three printed their quotes as ordinary text. The backslashes in the second string don't appear in the output: they only told Python that those quotes belong to the text.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| A double quote inside a double-quoted string | The string ends early and the line doesn't parse | Wrap it in single quotes, or write \" |
| An apostrophe inside a single-quoted string | The string ends at the apostrophe | Use double quotes, or write \': 'It\'s' printed It's |
Ending a triple-quoted string with a quote right before """ | The first three of the four quotes end the string, and the last one starts a new one: SyntaxError: unterminated string literal | Escape that quote, or switch to ''' |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
SyntaxError on a line with a string | A quote of the same kind as the outer ones ended the string | Change the outer quotes or escape the inner one |
| A backslash shows up in the output | It was doubled, \\, which means a real backslash | Use a single backslash before the quote |
FAQ
Should I use single or double quotes?
Either. Python treats them the same. Pick one for your project and switch only when the text contains that quote.
Is this how I should write JSON in Python?
For a fixed piece of JSON in the code, yes. When you build JSON from data, use json.dumps, which writes the quotes for you.
Conclusion
To get double quotes into a Python string, wrap it in single quotes or triple quotes, or escape each quote with a backslash. The same goes the other way for apostrophes.
Additional Resources
- String and bytes literals, in the Python language reference.
- Text, in the Python tutorial.
