Why doesn't Python give any error when quotes around a string do not match? Why doesn't Python give any error when quotes around a string do not match? python python

Why doesn't Python give any error when quotes around a string do not match?


The final """ is not recognized as a triple-quotation, but a single " (to close the current string literal) followed by an empty string ""; the two juxtaposed string literals are concatenated. The same behavior can be more readily recognized by putting a space between the closing and opening ".

>>> "not OK" ""'not OK'


"not OK"""

Python interprets this as "not OK"+""

If you give "not Ok""ay", you will get the output as 'not Okay'


You would think that there is no difference between " or ', but in reality, Python uses a greedy method to accept input.

Once Python sees a matching quotation, then that ends the statement.

It's why you can write something like "'s" "". Inside the string there is a ' but because you're in a string, python doesn't raise an error. Then after that, there is a " followed by " but that's a different (empty) string.

If you do something like "s' then Python is looking for that next " before if runs your command.