
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Double Quotes with 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
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
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
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"