How to Print Quotes in a String in Python?

By James L.

If you are a beginner in programming, then you may have found it quite tricky to print quotes in a string because Python doesn’t allow us to place double quotes inside of double quotes or single quotes inside of single quotes.

E.g. ""Hello"" or ''Hello'' throws a syntax error.

So in this guide, I will show you exactly how to print quotes in a string in Python.

To print quotes in a string in Python:

Use single quotes to wrap a string with double quotes, or vice versa

To print double quotes in a string in Python, we have to wrap the string with single quotes.

For example:

print('"Hello"')

Output:

"Hello"

To print single quotes in a string in Python, we have to wrap the string with double quotes.

For example:

print("'Hello'")

Output:

'Hello'

Use escape character \ before double or single quotes to print them in a string

We have to escape single or double quotes by placing an escape character \ before the single or double quotes.

E.g.

print("\"Hello\"")
print('\'Hello\'')

Output:

"Hello"
'Hello'

Use triple quotes to print single or double quotes

We have to use triple single quotes to print double quotes.

For example:

print('''"Hello"''')   #Output: "Hello"

Similarly, we have to use triple double quotes to print single quotes.

For example:

print("""'Hello'""")   #Output: ‘Hello’

We can use triple quotes to print single or double quotes in a multi-line string.

For example:

print("""He said, "JavaScript is awesome"
I replied, 'Python is more awesome'""")

Output:

He said, "JavaScript is awesome"
I replied, 'Python is more awesome'

If you want to print double quotes using triple double quotes then you have to write blank spaces before and after the double quotes. The problem with this approach is the space appears in the output console too.

print(""" "Hello" """)

Similarly, if you want to print single quotes using a triple single quotes string then you have to write blank spaces before and after the single quotes. The spaces also appear in the output console.

print(''' 'Hello' ''')

Let’s take a look at the example:

print("Hello")
print(""" "Hello" """)
print(''' 'Hello' ''')

Output:

Hello 
 "Hello" 
 'Hello' 
# Notice the spaces

You can also get quite creative with triple quotes.

For example:

print('''"He"ll"o"''')
print('''"He'll'o"''')
print("""'He"ll"o'""")
print("""'He'll'o'""")

Output:

"He"ll"o"
"He'll'o"
'He"ll"o'
'He'll'o'