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
"""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"
.