← Gideon Tzafriri

Quines

A quine is a program that prints its own source. This sounds easy until you try to write one from scratch. A program can print text, but the text it needs to print already contains the program that prints it. If we try to write the whole thing as a literal, the literal has to contain itself, which has to contain itself, and so on.

So a quine cannot merely store its whole source as a string. It has to store a smaller description of itself and then rebuild the full source from that description. In Python, the smallest useful shape is two statements on one line:

S = ...;print(...)

The first statement stores some text in S. The second prints the source line reconstructed from S.

There are two small traps here. First, the payload has to be bound to a name before the program can use it. A bare expression like this has no value for E:

print(E) + E

Second, even if E existed, print would not help us build a larger expression, because print returns None:

print('quine') + 'quine'   # TypeError

Printing is a side effect, not a string-producing operation. That is why the quine uses two statements: one to bind the payload, one to print the rebuilt source.

The real difficulty is not the word print. It is the quote. To print the source line, the program must print the quote marks around its own string literal. But a quote mark cannot appear raw inside a string delimited by the same quote mark, because it would close the string. The same problem appears with backslashes, which Python treats as escape characters.

The trick is to put those two awkward characters at known positions inside S. Let S begin with a backslash and then a quote. In source code, those two characters are written as \\\': two backslashes to produce one backslash, then an escaped quote to produce one quote. After that, S stores the body of the program.

Here is the print expression:

S[2:6] + S[1] + S[0]*3 + S[1:] + S[1] + S[-1] + S[6:]

Read it from left to right as a recipe for printing the source line:

S[2:6]   -> S =              the assignment head
S[1]     -> '                the opening quote
S[0]*3   -> \\\              the escaped backslash and escaped quote
S[1:]    -> 'S = print...    the stored quote and program body
S[1]     -> '                the closing quote
S[-1]    -> ;                the statement separator
S[6:]    -> print...         the live print statement

The important part is that the body of the program appears twice. Inside the string, it is data. After the semicolon, it is code. The two copies come from the same stored text: S[1:] contributes the quoted copy, and S[6:] contributes the executable copy.

Putting the pieces together gives this one-line Python quine:

S = '\\\'S = print(S[2:6] + S[1] + S[0]*3 + S[1:] + S[1] + S[-1] + S[6:]);';print(S[2:6] + S[1] + S[0]*3 + S[1:] + S[1] + S[-1] + S[6:]);

When Python runs it, S contains a backslash, a quote, and the text of the print statement. The print statement then manufactures the exact source line: first the assignment, then a quoted representation of S, then the semicolon, then the print statement itself. The final newline comes from print, as usual.

Conceptually, the line is a fixed point: a program whose output is its own description. Kleene’s recursion theorem explains why such fixed points exist in sufficiently expressive formal systems. The slicing construction above is the hands-on version: store the program’s text once, use it twice, and synthesize the two characters a string cannot plainly say about itself.