Update : Show From Python 3.6, you can use f-strings
Original Answer : You can try %-formatting
OR
OR escape the quote character with
And, if the double-quotes is not a restriction (i.e. single-quotes would do)
OR like others have already said (include it in your declaration)
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
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. ExampleLive Demo print(" ") print(" " " ") print(""aString"") OutputRunning 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. ExampleLive Demo print('Hello Tutorialspoint') print('"Hello Tutorialspoint"') OutputRunning the above code gives us the following result − Hello Tutorialspoint "Hello Tutorialspoint" Using string variablesWe can also use string formatting to print the double quotes as well as any other character which is part of the print syntax. ExampleLive Demo StringVar = 'Hello Tutorialspoint' print("\"%s\""% StringVar ) print("\%s\"% StringVar ) print('"%s"' % StringVar ) print('"{}"'.format(StringVar)) OutputRunning the above code gives us the following result − "Hello Tutorialspoint" \Hello Tutorialspoint\ "Hello Tutorialspoint" "Hello Tutorialspoint"
Updated on 26-Feb-2020 07:54:40
View Discussion Improve Article Save Article 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
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
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.. |