Generic selectors
Exact matches only
Search in title
Search in content
wb_sunny

Difference Between Single and Double Quotes in Python

Single Vs Double Quotation Marks

A String is a sequence of characters. You are allowed to start and end a string literal with single and double quotes in Python. There are two ways to represent a string in python programming.

In this article, you will see the difference between both the quotation marks with the help of an example i.e. code with its output.

What are single quotes used for in Python?

Single quotes are used to mark a quote within a quote or a direct quote in a news story headline.

When programming with Python, we generally use single quotes for string literals. For example – ‘my-identifier’. Let us understand with an example through code in Python.

NOTE: Always make use of single quotes when you know your string may contain double quotes within.

Example usage of single quotes in Python

Below is the code where you can see the implementation of single quote.

word = 'Ask?'
print(word)
sentence = 'Python Programming'
print(sentence)
name = '"Hi" ABC'
print(name)
congrat = 'We congrat's you.'
print(congrat)

Output

Ask?
Python Programming
Hi ABC
Invalid Syntax

What are double quotes in Python used for?

A double quotation mark is to set off a direct (word-for-word) quotation. For example – “I hope you will be here,” he said. In Python Programming, we use Double Quotes for string representation. Let us understand with an example through code in python.

NOTE: Use double quotes to enclose your strings when you know there are going to be single quotes within your string

Code

wish = "Hello World!"
print(wish)
hey = "AskPython says "Hi""
print(hey)
famous ="'Taj Mahal' is in Agra."
print(famous)

Output

Hello World!
Invalid Syntax
'Taj Mahal' is in Agra.

Key Differences Between Single and Double Quotes in Python

Single Quotation MarkDouble Quotation Mark
Represented as ‘ ‘Represented as ” “
Single quotes for anything that behaves like an Identifier.Double quotes generally we used for text.
Single quotes are used for regular expressions, dict keys or SQL.Double quotes are used for string representation.
Eg. ‘We “welcome” you.’Eg. “Hello it’s me.”

Bonus – Triple Quotes in Python

What if you have to use strings that may include both single and double quotes? For this, Python allows you to use triple quotes. A simple example for the same is shown below. Triple quotes also allow you to add multi-line strings to Python variables instead of being limited to single lines.

Example of triple quotes

sentence1 = '''He asked, "did you speak with him?"'''
print(sentence1)
sentence2 = '''"That's great", she said.'''
print(sentence2)

Output:

He asked, "did you speak with him?"
"That's great", she said.

As you can see, Python now understands that the double and single quotes are part of the string and do not need to be escaped.

Conclusion

To conclude this simple topic, I’d like to say this – the difference between single and double quotes in Python is not huge. It absolutely depends on the circumstances that we use single and double quotes in.

As a programmer, you can decide what fits best for your string declaration. And when in doubt, go for the triple quotes so you have no issues with what’s included within the string.