How to print double quotes with the string variable in Python?


Printing double quotes is tricky, as it itself is required as part of syntax to print the strings by surrounding them. In this article we will see how these double quotes can be printed using print statement.

The below scenarios will not print the double quote. The first two lines of code will give no output while the last one will through error.

Example

 Live Demo

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

Output

Running the above code gives us the following result −;

print(""aString"")
^
SyntaxError: invalid syntax

But if we surround the strings with proper quotes as shown below, then the quotes can themselves get printed. Enclosing double quotes within single quotes does the trick.

Example

 Live Demo

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

Output

Running the above code gives us the following result −

Hello Tutorialspoint
"Hello Tutorialspoint"

Using string variables

We can also use string formatting to print the double quotes as well as any other character which is part of the print syntax.

Example

 Live Demo

StringVar = 'Hello Tutorialspoint'
print("\"%s\""% StringVar )
print("\%s\"% StringVar )
print('"%s"' % StringVar )
print('"{}"'.format(StringVar))

Output

Running the above code gives us the following result −

"Hello Tutorialspoint"
\Hello Tutorialspoint\
"Hello Tutorialspoint"
"Hello Tutorialspoint"

Updated on: 26-Feb-2020

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements