Quine in Python

PythonProgrammingServer Side Programming

The Quine is a program, which takes no input, but it produces output. It will show it’s own source code. Additionally, Quine has some conditions. We cannot open the source code file inside the program.

Example code

Live Demo

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

Output

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

How this Quine is working?

Here a simple string formatting is working. We are defining a variable ‘a’, and inside a, we are storing ‘a=%r;print (a%%a)’ Then we are printing the value of a, and also replacing %r with the value of a. Thus the quine is working.

We can do the same task by opening the file like this.

print(open(__file__).read())

But in this case we are violating the rule of Quine. We cannot open file in Quine.

raja
Published on 14-Nov-2018 13:40:42
Advertisements