Last post of this year is a quine in python 3.

If you are interested in quines, you can find more ino in the post Quines: a new Hello World.

The quine I want to propose is a one line quine:

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

When you execute this script, its output is the script itself.

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

Some explanations are useful:

  • The operator x % y means substitute the value y in the format string x, same way as C printf.

Also note that the %% specifier stands for a literal % sign so s%%s within the format string will print as s%s, and will not capture a string.

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.

Here you are more quines in other languages: