How to print a variable with double quotes in python

Update :

From Python 3.6, you can use f-strings

>>> print(f'"{word}"')
"Some Random Word"

Original Answer :

You can try %-formatting

>>> print('"%s"' % word)
"Some Random Word"

OR str.format

>>> print('"{}"'.format(word))
"Some Random Word"

OR escape the quote character with \

>>> print("\"%s\"" % word)
"Some Random Word"

And, if the double-quotes is not a restriction (i.e. single-quotes would do)

>>> from pprint import pprint, pformat
>>> print(pformat(word))
'Some Random Word'
>>> pprint(word)
'Some Random Word'

OR like others have already said (include it in your declaration)

>>> word = '"Some Random Word"'
>>> print(word)
"Some Random Word"

Use whichever you feel to be better or less confusing.

And, if you need to do it for multiple words, you might as well create a function

def double_quote(word):
    return '"%s"' % word

print(double_quote(word), double_quote(word2))

And (if you know what you're doing &) if you're concerned about performance of these, see this comparison.


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"

How to print a variable with double quotes in python

Updated on 26-Feb-2020 07:54:40

  • Related Questions & Answers
  • How to insert records with double quotes in MySQL?
  • Single quotes vs. double quotes in C or C++
  • Find records with double quotes in a MySQL column?
  • Single and Double Quotes in Perl
  • What is the difference between single and double quotes in python?
  • How to Convert Double to String to Double in Java?
  • What is the difference between single and double quotes in JavaScript?
  • Is there a PHP function that only adds slashes to double quotes NOT single quotes
  • How to use quotes correctly while using LIKE with search variable in MySQL in Java?
  • Print newline in PHP in single quotes
  • How to print concatenated string in Python?
  • How to convert the Integer variable to the string variable in PowerShell?
  • Print Single and Multiple variable in Python?
  • How to print a string two times with single statement in Python?
  • Triple Quotes in Python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Many times, while working with Python strings, we have a problem in which we need to use double quotes in a string and then wish to print it. This kind of problem occurs in many domains like day-day programming and web-development domain. Lets discuss certain ways in which this task can be performed.

     Method #1 : Using backslash (“\”) This is one way to solve this problem. In this, we just employ a backslash before a double quote and it is escaped. 

    Python3

    test_str = "geeks\"for\"geeks"

    print("The string escaped with backslash : " + test_str)

    Output : 

    The string escaped with backslash : geeks"for"geeks

      Method #2 : Using triple quotes This is one more way in python to print and initialize a string. Apart from multiline comment, triple quotes are also good way of escaping. 

    Python3

    test_str =

    print("The string escaped with triple quotes : " + test_str)

    Output : 

    The string escaped with triple quotes : geeks"for"geeks

    The Time and Space Complexity for all the methods are the same:

    Time Complexity: O(1)

    Auxiliary Space: O(1)

    How do you print a double quote variable?

    Using \" escape sequence in printf, we can print the double quotes ("").

    How do I print a double quote number in Python?

    Use the escape character to print single and double quotes Use the escape character \ before double or single quotes to include them in the string.

    How do you print a variable within a quote in Python?

    For Python, you might use '"' + str(variable) + '"' .

    How do you do double quotes in Python?

    To put double quotes inside of a string, wrap the string in single quotes..
    double_quotes = '"abc"' String with double quotes. print(double_quotes) ... .
    single_quotes= "'abc'" String with single quotes. ... .
    both_quotes= """a'b"c""" String with both double and single quotes. ... .
    double_quotes = "\"abc\"" Escape double quotes..