How to Print Quotation Marks in Python?

Python does not allow double quotes within double quotes and single quotes within single quotes, which results in an error. It only enables opposing quotes, such as a double quote within a single quote and a single quote within a double quote.

Invalid quotes:

" "Python-programs" "

' 'Python-programs' '

Python Quotation Marks

1)Double quotes(” “)in Python

Printing double quotes is difficult because it is necessary as part of the syntax to enclose the strings. In this post, we’ll look at how to print these double-quotes using the print statement.

The double-quote will not be printed in the cases below. The first two lines of code will produce no output, while the last will produce an error.

Example1

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

Output:


Explanation:

It doesn't gove any output since double-quotes inside a double-quotes is Invalid

Example2

print(""Python-programs"")

Output:

 File "<ipython-input-12-ba874bef0698>", line 1
print(""Python-programs"")
^
SyntaxError: invalid syntax

So to print the double quotes use the below code

Example3

# Give the doule-quoted string as static input and store it in a variable
gvn_str = '"Welcome to Python-programs"'
# Print the given string
print(gvn_str)

Output:

"Welcome to Python-programs"

2)Single quotes(‘ ‘) in Python

Example1

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

Output:


Explanation:

It doesn't gove any output since single-quotes inside a single-quotes is Invalid

Example2

# Give the string as static input and store it in a variable
gvn_str = "'Welcome to Python-programs'"
# Print the given string
print(gvn_str)

Output:

'Welcome to Python-programs'

Example3

# Give the string as static input and store it in a variable
gvn_str = "good morning 'Python-programs' hello"
# Print the given string
print(gvn_str)

Output:

good morning 'Python-programs' hello

String variables in Python

String formatting can also be used to print the double quotes as well as any other character that is part of the print syntax.

gvn_str = 'Welcome to Python-programs'
print("\"%s\""% gvn_str )
print('"%s"' % gvn_str )
print("\\%s\\"% gvn_str )
print('"{}"'.format(gvn_str))

Output:

"Welcome to Python-programs"
"Welcome to Python-programs"
\Welcome to Python-programs\
"Welcome to Python-programs"