0

I'm so confused... why/how is a different from b?! Why don't they print the same thing?

>>> a = '"'
>>> a
'"'
>>> b = "'"
>>> b
"'"
7
  • 5
    I can't reproduce it. Neither on Python 2.7, nor 3.3. Commented Dec 6, 2012 at 8:58
  • No, I get False in the equality test. Commented Dec 6, 2012 at 9:01
  • 2
    @MartijnPieters It's not. The first string is comprised of a double quote, whereas the second string is comprised of a single quote. Clearly, ' and " are not equal. Commented Dec 6, 2012 at 9:02
  • 1
    Mehrdad, I do not understand the question, the strings are different, why should they print the same thing? Commented Dec 6, 2012 at 9:03
  • 2
    @phant0m: yeah, was not paying attention to that part. I thought everyone was talking about the quoting presentation not being different. :-P Commented Dec 6, 2012 at 9:03

1 Answer 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.

Sign up to request clarification or add additional context in comments.

3 Comments

To a complete Python noob like me it looks like the first variable contains a double quote (") and the second one a single quote ('). These string should be different indeed? (Not my downvote.)
From the examples in the question it seems that one of the strings contains ', while the other contains ". The quoting part is clear.
Now the poster has fixed the question this answer makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.