Search code examples
pythonquine

How does this Python 3 quine work?


Found this example of quine:

s='s=%r;print(s%%s)';print(s%s)

I get that %s and %r do the str and repr functions, as pointed here, but what exactly means the s%s part and how the quine works?


Solution

  • s is set to:

    's=%r;print(s%%s)'
    

    so the %r gets replaced by exactly that (keeping the single quotes) in s%s and the final %% with a single %, giving:

    s='s=%r;print(s%%s)';print(s%s)
    

    and hence the quine.