Quine in Python



The shortest possible queen in Python — it`s just one line of code!

For Python3.x

_ = `_ =% r; print _ %% _` ; print _ % _

_ = ` _ =% r; print _ (%%) _ ` ; print (_ % _)

Explanation:
The above code is the classic use of string formatting. First, we define a variable _ and assign it & # 39; _ =% r; print _ %% _ & # 39 ;. Second, we are printing _% _ . Here we are printing _ with _ as input to format the string. So % r in _ gets the value of _. You can even use % s instead of % r . We used double % in & # 39; _ =% r; typed _ %% _ & # 39; for escaping % .

But you can tell the code below is the smallest, right!

print open (__ file __). read ()

You should notice that this is indeed the smallest Python program that can print its own source code, but it`s not queen because quinn should not use the open () function to print your source code.

This article is courtesy of Sri Sanket Uppalapati . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.