0

私はとても混乱しています...なぜ/どのようにa違うのbですか?!なぜ彼らは同じものを印刷しないのですか?

>>> a = '"'
>>> a
'"'
>>> b = "'"
>>> b
"'"
4

1 に答える 1

5

The strings are not presented differently. Their presentation is just adjusted to avoid having to quote the contained quote. Both ' and " are legal string literal delimiters.

Note that the contents of the string are very different. " is not the same string as '; a == b is (patently) False.

Python would have to use a \ backslash for the " or ' character otherwise. If you use both characters in a string, then python is forced to use quoting:

>>> '\'"'
'\'"'
>>> """Tripple quoted means you can use both without escaping them: "'"""
'Tripple quoted means you can use both without escaping them: "\''

As you can see, the string representation used by Python still uses single quotes and a backslash to represent that last string.

于 2012-12-06T08:56:11.497 に答える