How to print double quotes 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. Show 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) Update : 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. How do you show double quotes in Python?Python does have two simple ways to put quote symbols in strings.. You are allowed to start and end a string literal with single quotes (also known as apostrophes), like 'blah blah' . Then, double quotes can go in between, such as 'I said "Wow!" to him. ... . You can put a backslash character followed by a quote ( \" or \' ).. How do you print a sentence with a double quote 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 double quotes?\" - escape sequence
Since printf uses ""(double quotes) to identify starting and ending point of a message, we need to use \" escape sequence to print the double quotes.
How do you print double and single quotes in Python?If you want to use both single- and double-quotes without worrying about escape characters, you can open and close the string with three double-quotes or three single-quotes: print """In this string, 'I' can "use" either. """ print '''Same 'with' "this" string!
|