Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

double quotes in string representation

This snippet:

formatter = "%r %r %r %r"
print formatter % (
    "I had this thing.",
    "That you could type up right.",
    "But it didn't sing.",
    "So I said goodnight."
)

when run, prints this string:

'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'

Why did "But it didn't sing." get put in double quotes when the other three items were in single quotes?

(This code is taken from Learn Python the Hard Way Exercise 8.)

like image 467
Joseph Potts Avatar asked Aug 11 '12 19:08

Joseph Potts


People also ask

How do you display double quotes in a string?

To place quotation marks in a string in your code In Visual C# and Visual C++, insert the escape sequence \" as an embedded quotation mark. For example, to create the preceding string, use the following code. Insert the ASCII or Unicode character for a quotation mark. In Visual Basic, use the ASCII character (34).

Do strings use double quotes?

Both single (' ') and double (" ") quotes are used to represent a string in Javascript. Choosing a quoting style is up to you and there is no special semantics for one style over the other. Nevertheless, it is important to note that there is no type for a single character in javascript, everything is always a string!

Why do we use double quotes for strings?

The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string.

What do the quotation marks around a string indicate?

Strings in JavaScript are contained within a pair of either single quotation marks '' or double quotation marks "". Both quotes represent Strings but be sure to choose one and STICK WITH IT.


1 Answers

Python is clever; it'll use double quotes for strings that contain single quotes when generating the representation, to minimize escapes:

>>> 'no quotes'
'no quotes'
>>> 'one quote: \''
"one quote: '"

Add a double quote in there as well and it'll revert back to single quotes and escape any single quotes contained:

>>> 'two quotes: \'\"'
'two quotes: \'"'
like image 81
Martijn Pieters Avatar answered Sep 28 '22 12:09

Martijn Pieters