Python One Line Quine

Most computer scientists, programmers, and hackers don’t even know the meaning of the word “Quine” in the context of programming. So, first things first:

What is a Quine?

Roughly speaking, a quine is a self-reproducing program: if you run it, it generates itself.

Here’s a great definition:

:quine: /kwi:n/ /n./ [from the name of the logician Willard van Orman Quine, via Douglas Hofstadter] A program that generates a copy of its own source text as its complete output. Devising the shortest possible quine in some given programming language is a common hackish amusement. (source)

The name “quine” was coined by Douglas Hofstadter, in his popular science book Gödel, Escher, Bach, in honor of philosopher Willard Van Orman Quine (1908–2000), who made an extensive study of indirect self-reference, and in particular for the following paradox-producing expression, known as Quine’s paradox.


The shortest possible quine is the following empty program:


The program is self-reproducing because the output of the program is the program itself. Go ahead and run it in your own shell! 😉

Let’s dive into a collection of Python Quines to demonstrate how they work!

Python One-Liner Quine 1

Here’s a short one-liner Quine, I found at this resource:


Here’s the code in an interactive shell so that you can play with this Quine in your browser:

Exercise: Run the code. What’s the output? Can you explain why?

Python One-Liner Quine 2

The following Quine is also interesting. I found it here when researching this article. The original source cited it as “the shortest possible Quine”—which is not accurate! You’ve already seen a shorter one as our first example in this article. However, the one-liner is interesting nonetheless:

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

Again, here’s the interactive code shell that allows you to test if this is really a Quine!

Again, you use Python string formatting to first define a variable named _. The single underscore is a perfectly valid variable name

Second, you assign the code '_=%r;print _%%_' to the newly created underscore variable.

Third, you print the expression _%_ which consists of the underscore variable _ with _ as input to string formatting (how meta!).

Thus, the string formatting expression %r in _ gets the value of _.

Note that the double % in '_=%r;print _%%_' escapes the percentage symbol.

Python One-Liner Quine 3

Some would say that the following code also fits the definition of the Quine. The code reads its own file and prints the result to the shell:


The output is the code itself—this is trivially fulfilled because the code reads the file it is stored and it prints the result. However, some would consider this “cheating” because Quines are supposed to create itself internally using only code and not meta mechanisms such as reading files.

Here’s the interactive shell:

Exercise: change the code and add an extra line. Is it still a Quine?

Python One Liner Quine 4

The following Quine comes from a SO answer and it’s one of my favorites:

print((lambda x:f"{x}{x,})")('print((lambda x:f"{x}{x,})")',))

Try it yourself in the interactive code shell:

Exercise: Try to change at least one character and still keep it a Quine!

The Quine creates an anonymous lambda function and calls it immediately passing one string value. The way the lambda function modifies the string value ensures that the output of the code is equal to the code itself.

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!