Single and Double Quotes | Python

Last Updated : 27 Dec, 2025

In Python, strings can be created using single quotes (' ') or double quotes (" "). Both methods are valid and work in the same way. The choice between single and double quotes often depends on readability and how quotes are used inside the string.

You can create a string using either single or double quotes:

Python
a = 'Python'
b = "Python"

Both statements create the same type of string. Python does not prefer one over the other.

When Quotes Cause Errors

While working with strings, errors can occur if the text inside the string contains the same type of quote used to define it. In such cases, Python may get confused about where the string actually ends.

Python
print('It's Python')

Output

ERROR!
Traceback (most recent call last):
File "<main.py>", line 1
print('It's Python')
^
SyntaxError: unterminated string literal (detected at line 1)

Explanation: The apostrophe (') in It's is treated as the end of the string. Python then sees the remaining text as invalid syntax.

Correct Ways to Fix It

To avoid this errors, Python allows you to switch between single and double quotes. This makes it easy to include one type of quote inside the string without confusing the interpreter.

Python
print("It's Python!")

Output
It's Python!

Explanation: Using double quotes allows the single quote inside the string to be interpreted correctly.

Using Single and Double Quotes Together

There are situations where you want to display quotes as part of the output. In such cases, mixing single and double quotes makes the code easier to read. The following examples show how to print text that includes quotes by choosing the appropriate outer quotation marks.

Python
# Printing text within single quotes
print("'WithQuotes'")
print("Hello 'Python'")

# Printing text within double quotes
print('"WithQuotes"')
print('Hello "Python"')

Output
'WithQuotes'
Hello 'Python'
"WithQuotes"
Hello "Python"

Explanation:

  • When the string is enclosed in double quotes, single quotes inside do not need escaping.
  • When the string is enclosed in single quotes, double quotes can be used freely inside.
  • This approach avoids unnecessary escape characters and improves readability.
Comment
Article Tags:

Explore