46

Python 2.x (30 bytes):

_='_=%r;print _%%_';print _%_

Python 3.x (32 bytes)

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

Is this the shortest possible Python quine, or can it be done better? This one seems to improve on all the entries on The Quine Page.

I'm not counting the trivial 'empty' program.

12
  • 2
    Won't work with Python 3.x, by the way. Jun 3, 2011 at 5:27
  • 3
    "can it be done better?" Although this question has definitively an answer it is hard to be answered (unless the answer is yes and you have a counterexample). How should someone know without testing ALL possible shorter programs?
    – Howard
    Jun 3, 2011 at 5:27
  • 2
    @Howard: At some point, there aren't all that many of those...
    – Nemo
    Jun 3, 2011 at 5:32
  • 11
    For the record, _='_=%r;print(_%%_)';print(_%_) works in python3. Jul 12, 2011 at 2:28
  • 4
    I'd prefer to write it as r='r=%r;print r%%r';print r%r (for python2), though. Jul 12, 2011 at 2:53

11 Answers 11

72

I'm just going to leave this here (save as exceptionQuine.py):

    File "exceptionQuine.py", line 1
        File "exceptionQuine.py", line 1
        ^
IndentationError: unexpected indent
5
  • 2
    Why do you have to save as exceptionQuine.py? The error output is the code regardless of the filename, no? Jan 23, 2016 at 0:49
  • 4
    No. Let's say I save the file as foo.py, the output will be: File "foo.py", line 1 File "exceptionQuine.py", line 1 ^ Jan 24, 2016 at 2:09
  • 17
    Very clever. However I'm not sure that stderr is counted as output :-) Also, are you being a little jocular with your reply to Quinn regarding the filename? Jan 28, 2016 at 8:25
  • 5
    I don't care what anyone says, this answer is the single best one. Sep 9, 2020 at 4:35
  • How is this shorter than the example provided by the asker?
    – Sparkette
    Sep 4, 2021 at 3:05
23

Technically, the shortest Python quine is the empty file. Apart from this trivial case:

Since Python's print automatically appends a newline, the quine is actually _='_=%r;print _%%_';print _%_\n (where \n represents a single newline character in the file).

5
14

Both

print open(__file__).read()

and anything involving import are not valid quines, because a quine by definition cannot take any input. Reading an external file is considered taking input, and thus a quine cannot read a file -- including itself.

For the record, technically speaking, the shortest possible quine in python is a blank file, but that is sort of cheating too.

1
  • 5
    "For the record, technically speaking, the shortest possible quine in python is a blank file, but that is sort of cheating too." Correction: it is cheating.
    – EKons
    Apr 14, 2016 at 13:51
13

In a slightly non-literal approach, taking 'shortest' to mean short in terms of the number of statements as well as just the character count, I have one here that doesn't include any semicolons.

print(lambda x:x+str((x,)))('print(lambda x:x+str((x,)))',)

In my mind this contends, because it's all one function, whereas others are multiple. Does anyone have a shorter one like this?

Edit: User flornquake made the following improvement (backticks for repr() to replace str() and shave off 6 characters):

print(lambda x:x+`(x,)`)('print(lambda x:x+`(x,)`)',)
5
  • 1
    This comment explains the OP's interest for small size in bytes.
    – EKons
    Apr 19, 2016 at 14:54
  • 1
    This one is my favorite! Dec 25, 2016 at 5:44
  • 1
    Note that backticks are repr, not str. May 28, 2017 at 5:16
  • Thanks @CalculatorFeline. I edited it to incorporate that tidbit.
    – postylem
    Jul 17, 2017 at 14:29
  • 5
    Nice! Here's Python 3.6 with f-strings: print((lambda x:f"{x}{x,})")('print((lambda x:f"{x}{x,})")',))
    – user4698348
    Feb 28, 2018 at 1:35
11

Even shorter:

print(__file__[:-3])

And name the file print(__file__[:-3]).py (Source)

Edit: actually,

print(__file__)

named print(__file__) works too.

4
  • 5
    Nice abuse of the rules ;)
    – Tino
    Feb 22, 2015 at 4:40
  • 1
    upvote, it's good :) I can't help but feel quine program is nicer if it works independently of the filename though
    – wim
    Feb 18, 2016 at 3:45
  • This cheat works with gcc, too (following needs bash): a=$'main(){write(1,__FILE__,29);}'; echo -n "$a" > "$a.c"; gcc "$a.c"; ./a.out | cmp - "$a.c"; echo $? (note, this does not contain any linebreaks)
    – Tino
    Dec 20, 2016 at 17:38
  • 15 bytes+15 bytes for filename=30 bytes by PPCG scoring. Not quite good enough. May 28, 2017 at 5:18
7

Python 3.8

exec(s:='print("exec(s:=%r)"%s)')
5
6

Here is another similar to postylem's answer.

Python 3.6:

print((lambda s:s%s)('print((lambda s:s%%s)(%r))'))

Python 2.7:

print(lambda s:s%s)('print(lambda s:s%%s)(%r)')
1
  • ah yes, a little nicer than mine perhaps.
    – postylem
    Jul 2, 2019 at 16:37
3

As of Python 3.8 I have a new quine! I'm quite proud of it because until now I have never created my own. I drew inspiration from _='_=%r;print(_%%_)';print(_%_), but made it into a single function (with only 2 more characters). It uses the new walrus operator.

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

2

I would say:

print open(__file__).read()

Source

6
  • 9
    That's not really in the spirit of the question.
    – Nick ODell
    Jun 3, 2011 at 5:34
  • Oh, and if you put backslashes before the parens, the link in your post won't be broken.
    – Nick ODell
    Jun 3, 2011 at 5:37
  • 9
    That breaks the rules of quines. A quine is a program that writes its own source as an output without reading it the original source file.
    – panzi
    Jul 2, 2014 at 1:44
  • Where are the rules of quines? Can you find a set of rules for quines that does not include that rule? Aug 26, 2014 at 21:20
  • Can you find a set of rules for quines that include themselves? Mar 12, 2015 at 11:13
0

This one is least cryptic, cor is a.format(a) a="a={1}{0}{1};print(a.format(a,chr(34)))";print(a.format(a,chr(34)))

-25

I am strictly against your solution.

The formatting prarameter % is definitively a too advanced high level language function. If such constructs are allowed, I would say, that import must be allowed as well. Then I can construct a shorter Quine by introducing some other high level language construct (which, BTW is much less powerful than the % function, so it is less advanced):

Here is a Unix shell script creating such a quine.py file and checking it really works:

echo 'import x' > quine.py
echo "print 'import x'" > x.py
python quine.py | cmp - quine.py; echo $?

outputs 0

Yes, that's cheating, like using %. Sorry.

15
  • 12
    % is just a language function, like concatenation or multi-line strings. Using external resources (like an extra file that's not included in the quine) is plain cheating.
    – BoppreH
    Oct 24, 2013 at 1:21
  • 5
    Do you outlaw %s in C quines, too? Apr 20, 2014 at 4:18
  • 6
    As far as I am aware, the purpose of a quine is to write a program that outputs its own source without external inputs. % does not use an external source. Import can be viewed in two ways. Either you consider the second file an external source (thus breaking the rules) or you consider the imported code as part of the source, in which case your quine does not print out the "full source". Thus % does not violate quine rules, and your import does. Feb 20, 2015 at 1:12
  • 1
    @Tino You "outlawed" import statements here
    – EKons
    Apr 19, 2016 at 15:04
  • 2
    @Tino I can't seem to understand you I guess. Also, why shouldn't it be allowed? It's just that you can't read input and you can't have an empty program.
    – EKons
    Dec 20, 2016 at 18:12

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.