ChatGPT解决这个技术问题 Extra ChatGPT

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

I've started learning Python recently and I don't understand why Python behaves like this:

>>> "OK"
'OK'
>>> """OK"""
'OK'
>>> "not Ok'
  File "<stdin>", line 1
    "not Ok'
           ^
SyntaxError: EOL while scanning string literal
>>> "not OK"""
'not OK'

Why doesn't it give an error for the last statement as the number of quotes does not match?

In case you prefer formal documentation, it's here.
Just to offer some more insight: I think what confused you is that you interpreted """OK""" to be a "quote within a quote within a quote" "("("OK")")", but that's not what it is. A triple-quote (""" or '''), although consisting of three characters, is treated as a single symbol that starts (and ends) a multi-line verbatim string (see this). You can't really nest quotes; in something like "foo 'bar'" or 'foo "bar"', the inner quotes are treated as ordinary chars, so this will work too "foo ' bar".

c
chepner

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'

To further drive this home: >>>"ok"'' and >>>'ok'""
Aside: @DukeSilver Don't think putting >>> in front of code snippets works as well in comments as it does in questions and answers. To some it may look like a bizarre alien operator you're using, whereas "ok"'' is perfectly legible.
@PaulEvans That's the marker that the Python console puts in front of each line you type in.
@nick012000 I know, that's why it works fine in questions and answers. My point is that it's unnecessary and potentially confusing in comments.
P
Pranav Hosangadi

"not OK"""

Python interprets this as "not OK"+""

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


"""""""" ok "" File "", line 1 """""""" ok "" ^ SyntaxError: invalid syntax. What about """ok" ? it give me something bizarre
"""ok" , So in python, """ triple quotes are used to create multi line strings if required. So when python finds a triple quote, then it looks for the ending triple quote of that beginning triple quote.
When Python scans for the opening quote, it prefers triple-quotes over ordinary quotes. But once the opening quote is determined, the closing quote is also determined; so if the string started with a plain double-quote then only one double-quote is used on the other end, and anything after that is part of the next token.
Z
ZexalDaBeast

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.


Why do you call this method "greedy"?
Because that's a general description of a class of algorithms that take "the first thing that fits", even if a later option would be better.
That's not what "greedy" means. Greedy means when you make a regex like A.*B to match a string, but it ends up matching things like ABBB instead of just AB because the regex is "greedy".
Seconding Pat-Laugh above: this is in fact the exact opposite of greedy: this is a lazy algorithm.
It's not completely lazy, though; """ start quotes are greedy. (Most things are lazy, though.)
A
Aroo

Python uses like a stack implementation to detect quotes opening and closing. If you know whats a stack is, its a datastructure, in which Last element will be removed first.

Assume your string is A = "''" What it does is, for every single quote or double quote encountered first time, it will add it the stack, and for every second, it will remove from the stack, unless its ofcourse, """ which will be parsed as single one

In our example, A = "''", iterating over it, for the first 2 elements, they will be added to stack and for the next 2, they will be removed.

So the quotes will be matched, if and only if, the number of elements in the stack in the end must be zero


So if the string was "'" it wouldn' t allow it because the stack is not empty after parsing all the string?? This is not correct.

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now