How to write your first Quine program

A quick guide to self replicating programs in Python

A quine-relays: a Python program generates a Java program that generates a Ruby program that generates the first Python program

Aim of this guide is to show how to write a program that outputs its own source code with no input. This family of programs is called Quine.

Why is this interesting? Well, because self-referencing stuff tends to be always interesting 🙂

There are many ways to build such programs, but the most common way is called constructive method.

But first of all…this is not a valid Quine program:

print open(__file__).read()
This is not Quine, is cheating in Python

A program can be called Quine only if it takes no input at all. In this case, the program reads its source code as file, thus making it using itself as input. Also this guide doesn’t consider trivial Quine programs (e.g. 0 bytes programs or self-evaluating atoms like in Lisp).

A program that has itself as output

Constructive Quine

The most effective and understandable way is to divide the program into 2 parts:

  • The code delegated to print the information (like the print() function in Python, System.out.println() in Java or the global object std::coutin C++).
  • A data structure (such as a string or a list) that can store part of the source code. Notice that even the quotes surrounding the strings need to be printed by the program.

These requirements must be adjusted and calibrated depending on the specification of the language you want to use. Since each language uses different mechanisms to manage data structures and printings and it is not possible to cover here all the variations, the guide focuses just on the Python specification.

Pythons str() vs repr()

If you are already aware of this mechanic, then you can skip this section.

Otherwise, this is very important not only to fulfil our task but to better understand an interesting point of the language itself.

str() — human readable object

print(str(‘Hello World!’))

str() is a standard function in Python that returns a string version of an object. It is supposed to generate a friendly human readable string.

repr() — evaluable object

print(repr(‘Hello World!’))
Notice the quotes surrounding the string

repr() on the other hand returns an evaluable version of the string representing the object (via eval() function). For strings, it returns the string itself surrounded by quotes.

The same can be written in printf fashion by using the following syntax, where %r indicates to format with repr() the string after the second %.

print(‘%r’%’Hello World!’)

Quine via evaluation

This technique involves usage of the eval() function. The main pattern of the program we are going to create is the following:

variable is our data structure and inside we must put a piece of code that prints and evaluates itself. Why do we want to have the eval function also in the string? Because the last line of the program uses eval() and we must track this down in the string.

Since the program starts with a variable=', we have to put this part as first thing to print. Also, for the same reason, the last section to be printed must be eval(variable).

The execution prints variable = ??? eval(variable), which is fine but we have to print also the print() function, the quotes and the break line. We cannot write again inside the string a print(...) otherwise we will enter in an infinite recursive loop by adding print(...) indefinitely.

So the main idea is to use again variable because it already contains the print() function! Just concatenate it as shown:

The output is very similar to the source code, but we have to handle the quotes surrounding the print() function and the break line. Luckily we now now that Python provides the repr() function to return an evaluable version of a string and for the break line we can just add an escaped \n:

The Quine program perfectly working

And here it is your first Quine program in Python, since there is no input and the output is exactly the source code of the program!

Other Quine programs

There are other ways to create Quine programs and you can find below a short description of some of them.

Shortest Python Quine

This is the most famous Quine program in Python and it is also the shortest non-trivial ever produced. Notice that it doesn’t use eval() but plays just with repr() in printf fashion. Not so different from our version indeed since it uses a constructive method.

Error Quine

This program must be save as

This is interesting. The program is an invalid Python code that generates an IndentationError. It outputs itself but in stderr standard stream and not in stdout (in my opinion this is a still valid Quine).


A Quine-relay is a program written in a language that outputs a program in another language that generates in turn the starting program.

A visual example is given by the featured image of this article.

Source: towardsdatascience