A quine is a computer program that ‘knows’ its own entire source code via indirect self-reference, rather than by getting it as input. The classic exercise in this domain is writing a (non-empty) program that takes no input and which outputs its own entire source code. But the same trick can be used to do more than that; for any program that takes a string as input and performs some operations on it, we can write a quining program that takes no input and performs the same operations on the program’s own source code.

The trick to write a quining program is to take a recipe for substituting a string into various places in a program, and then to apply this recipe to itself. If you’ve not encountered this idea before, but you know a programming language, it is a nice exercise to try and write a quine which prints its own source code.

An example of a quine in Python (due to Nate Soares) which prints its own source code:

template = 'template = {hole}\nprint(template.format(hole=repr(template)))'

Wikipedia has a [list of examples of quining programs](https://​​en.wikipedia.org/​​wiki/​​Quine_(computing)#Examples) in various languages.

Named after the logician W.V.O. Quine, as coined by Douglas Hofstadter in Gödel, Escher, Bach, in the informal context of English phrases like

“Yields falsehood when preceded by its quotation” yields falsehood when preceded by its quotation.

Quining has been used for practical purposes and even seen in the wild, most famously in [Ken Thompson’s illustration of a C compiler Trojan backdoor](http://​​en.wikipedia.org/​​wiki/​​Backdoor_(computing)#Compiler_backdoors).