raw strings and \

Blackbird fake at nospam.no
Mon Mar 6 13:59:31 CET 2006

Fredrik Lundh wrote:
> "Blackbird" <fake at nospam.no> wrote:
>> Slightly OT, but here is a crazy little program that shows the power
>> of using raw strings:
>> s=r'print "s=r\'%s\'\n%s"%(s,s)'
>> print "s=r\'%s\'\n%s"%(s,s)
>> When run, this program will print an exact copy of itself.
> I'm not sure what the raw strings brings to the table, though; it's
> not like you need them to write a "self-replicating" python program:
>     http://miscoranda.com/37

Great link!  I wasn't aware that this was called a Quine.  This one is

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

When I hacked down the two lines, I didnt see that assigning the variable to
the beginning of the program, instead of the end, essentially gets rid of
literals in the actual print statement.  And now I also learned about %r.

More information about the Python-list mailing list