Quine in Python

July 28, 2003 | My Projects | By: Mark VandeWettering

I’ve been trying to hone my Python skills. Python has lots of nice commands for manipulating text, including standard libraries for doing base64 encoding. After a few abortive tries, I came up with.

#!/usr/bin/env python
data="""
aWYgX19uYW1lX18gPT0gJ19fbWFpbl9fJzoKICAgIGltcG9ydCBzeXMsIGJhc2U2NAogICAgc3lz
LnN0ZG91dC53cml0ZSgnIyEvdXNyL2Jpbi9lbnYgcHl0aG9uXG5kYXRhPSIiIicrZGF0YSsnIiIi
XG4nKQogICAgc3lzLnN0ZG91dC53cml0ZShiYXNlNjQuZGVjb2Rlc3RyaW5nKGRhdGEpKQo=
"""
if __name__ == '__main__':
    import sys, base64
    sys.stdout.write('#!/usr/bin/env python\ndata="""'+data+'"""\n')
    sys.stdout.write(base64.decodestring(data))

(You can download the code directly from here.) It’s very similar to one of my early C quines, but significantly shorter, and somewhat easier to follow.

For other good links on quines, try this page of quines, or perhaps this list of quines in many programming languages.