Quines
Any Turing-complete language admits a program called a quine that reproduces its own source, a consequence of Kleene's recursion theorem, and Python is no exception. Our goal is to build one by hand, on a single line of vanilla Python 3.
The line has to do two things at once: run a print that emits text, and carry the text it emits. The catch is that the text it emits is the line itself, which already contains the print. A plain literal cannot escape this regress. Writing print('...the whole line...') would need a string holding the entire line, including the string, including the string, without end. So the printed text cannot simply be the line; the line has to be rebuilt from something smaller.
Two failed attempts pin down the mechanics before we get there. Say we have an expression E to print and we try to keep a second copy alongside the print:
print(E) + E
This fails twice, and both failures are informative. First, E is not a defined name, so Python raises a NameError: the payload has to be bound to a name before it can be used. Second, even with a value in hand the line is still broken, because print returns None:
print('quine') + 'quine' # TypeError: NoneType + str
Printing is a side effect, not a value, so we cannot fuse a print call to anything else with an operator. The print and the payload have to be two separate statements, and Python lets us put two statements on one line with a semicolon:
S = 'quine'; print(S)
The first statement binds the payload to a name; the second prints something built from it. A dangling expression such as ; 'quine' would be evaluated and thrown away, printing nothing in a script even though the REPL would echo it, so the second piece has to be a real print and the first a real binding.
Now the actual problem comes into focus. If print emits a literal, it emits that literal and nothing of the code around it. To emit the surrounding code as well, the code's own text must live inside S, and the print must reassemble the whole line from it: the S = '...' wrapper, the literal form of S, and the print statement itself. In effect S gets rendered twice, once as data in its quoted form and once as code in the statements it spells out.
That last requirement is where the difficulty really lives. Rendering S as a literal means re-emitting the quote that delimits it, and that quote cannot appear raw inside the string without closing it early. The same holds for any backslash, which Python reads as the start of an escape. Solving the quine reduces to exactly this: reproducing the delimiting quote and the backslashes from inside the string with the correct escaping.
The way out is to stop trying to type the quote and the backslash in the print at all. If S already holds a quote character, written in the source as the escape \', then that quote sits at a fixed position in the value of S, and we can pull it out by index. S[k] hands us a quote to drop anywhere in the output, and it never has to appear raw inside the expression. A backslash stored as \\ works the same way. So the awkward characters get written once, inside the string where escaping is legal, and are reached everywhere else by position.
Take the smallest layout that does this. Let the value of S begin with a backslash and then a quote, which in the source is written '\\\'...': the opening ', then \\ for the backslash at index 0, then \' for the quote at index 1. After those two characters, store the rest of what the line needs, namely the text S = print(E); , where E is the print expression we are about to write. Keep E free of quotes and backslashes, built only from letters, digits, brackets, colons, +, *, parentheses, semicolons and spaces. Then the text of E is identical whether it is read as data inside S or run as code, and the only characters that ever needed special handling are the quote and backslash we just isolated at the front.
Now write the print by walking the target line from left to right and reading each piece off S:
E = S[2:6] + S[1] + S[0]*3 + S[1:] + S[1] + S[-2] + S[6:]
S[2:6] S = the assignment head
S[1] ' opening quote of the literal
S[0]*3 \\\ three backslashes: the escaped backslash, then the first half of the escaped quote
S[1:] 'S = print(E); the quote that completes the escape, then the stored body
S[1] ' closing quote
S[-2] ; the statement separator
S[6:] print(E); the live statement
The economy of the trick is in the two slices that both return the body: the text of E is stored once inside S, yet it appears twice in the output, as the in-quote copy from S[1:] and as the live statement from S[6:]. Concatenating the seven pieces in this order spells the source line back exactly, and it closes the loop, since the expression we just wrote is precisely the body we stored in S. One detail makes the indices line up: the single trailing space appended to S puts the semicolon at index -2, so S[-2] lands on it rather than on the space. Put together, the whole thing is one line that prints itself:
S = '\\\'S = print(S[2:6] + S[1] + S[0]*3 + S[1:] + S[1] + S[-2] + S[6:]); ';print(S[2:6] + S[1] + S[0]*3 + S[1:] + S[1] + S[-2] + S[6:]);
Everything beyond this is cosmetic. Prepend any four characters in front of the backslash, say the leftover name bob=, and every index shifts: the backslash moves to 4, the quote to 5, the S = to indices 6 through 9, and print to 10. The padding is now part of the literal content, so it has to be reproduced too, which is the single extra term S[:4]. Nothing about the mechanism changes, and we arrive at the line we set out to build:
S = 'bob=\\\'S = print(S[6:10] + S[5] + S[:4] + S[4]*3 + S[5:] + S[5] + S[-2] + S[10:]); ';print(S[6:10] + S[5] + S[:4] + S[4]*3 + S[5:] + S[5] + S[-2] + S[10:]);
The line ends in a single trailing space, the same one appended to S, and that is what makes the printed copy match the source character for character. The name bob carries no meaning; any four characters would do, as long as a backslash lands at index 4 and a quote at index 5.
Conceptually the finished line is a fixed point of the map "given a source, print that source." Kleene's recursion theorem promised such a fixed point exists in any language as expressive as Python. The slicing exhibits one by hand: store the program's own text once, render it back as both data and code, and manufacture the two characters, the quote and the backslash, that a string cannot state plainly about itself.