Skip to main content

How to Put Quotes Inside a String in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
'<text with "double quotes">'
"<text with 'single quotes'>"
"<text with \"escaped\" double quotes>"
"""<text with 'both' "kinds">"""
PartWhat 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:

main.py, which stores JSON with double quotes in single quotes, a sentence with escaped double quotes, and a triple-quoted string with an apostrophe and double quotes, then prints all three.

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​

Terminal
uv run python main.py
Output
{"name": "Ada", "role": "admin"}
She said "hi" and left.
It's "fine".

A zsh terminal where uv run python main.py prints the JSON with its double quotes, She said "hi" and left., and 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​

MistakeWhat happensDo this instead
A double quote inside a double-quoted stringThe string ends early and the line doesn't parseWrap it in single quotes, or write \"
An apostrophe inside a single-quoted stringThe string ends at the apostropheUse 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 literalEscape that quote, or switch to '''

Troubleshooting​

SymptomLikely causeFix
SyntaxError on a line with a stringA quote of the same kind as the outer ones ended the stringChange the outer quotes or escape the inner one
A backslash shows up in the outputIt was doubled, \\, which means a real backslashUse 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​