Change language

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.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method