What does this Python program do?

In summary, the Python program provided takes a given string and uses the %r or repr() function to return a raw string in python syntax that can be used to recreate the input value. It then substitutes the string s into the string s at the point of the %s, resulting in a program that prints itself in proper python syntax. This concept is known as a Quine in computing and can be achieved in other languages as well.
  • #1
Demystifier
Science Advisor
Insights Author
Gold Member
14,239
6,717
Can someone tell me what does this Python program do? :wink:
Python:
s = 's = %r\nprint(s%%s)'
print(s%s)

Mentor: add html code tags
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Well its printing itself.

Here's an explanation of the %r or repr() function which returns a raw string in python syntax that can be used to recreate the input value.

https://stackoverflow.com/questions/6005159/when-to-use-r-instead-of-s-in-python

so start with the print statement its going to substitute the string s into the string s at the point of the %s in the string s.

print(s%'a') would produce
Python:
s = 'a'
print(s%s)

The end result is a program that prints itself in proper python syntax so you can run it again. Its kind of a recursive loop except the looping is missing.
 
Last edited:
  • Like
Likes QuantumQuest and Demystifier
  • #3
This reminds me of the early 1980's story of a kid in school who wrote a program in BASIC that simulated the command prompt and printed fake answers. It responded to 'list' to print a fake program and to 'run' to give a fake answer.

Code:
$ list
10 print 1+1
$ run
3
$ 10 print 1+2
$ run
5
$ list
10 print 1+2
...

The teacher was completely baffled.

I discovered another one online that was truly epic with a fake DOS shell:

http://stevehanov.ca/blog/index.php?id=79
 
  • #5
Demystifier said:
A program which prints itself is possible in any program language, but it seems that in no other language such a program looks so simple: https://en.wikipedia.org/wiki/Quine_(computing)

Well it works because Python supports printing objects in their literal (source) form. Python isn't the first language to support this. This Lisp example works exactly the same way as the Python version:
Code:
(let ((s "(let ((s ~s))~%  (format t s s))~%"))
  (format t s s))
One of the examples on Rosetta Code uses a reader macro to slightly shorten this (slightly modified here):
Code:
(format t #1="(format t #1=~s #1#)~%" #1#)
 
  • Like
Likes QuantumQuest and Demystifier

Related to What does this Python program do?

1. What is Python?

Python is a high-level programming language that is widely used in various fields such as data science, web development, and artificial intelligence. It is known for its simple and readable syntax, making it a popular choice for beginners and experienced programmers.

2. How do I run a Python program?

To run a Python program, you will need to have Python installed on your computer. Once installed, you can open a command prompt or terminal, navigate to the directory where your Python program is saved, and type "python your_program.py" to execute the program.

3. What does the "print" function do in Python?

The "print" function in Python is used to display output on the screen. It can print strings, numbers, and variables, making it a useful tool for debugging and displaying results in a program.

4. How can I learn Python?

There are many resources available for learning Python, including online tutorials, books, and courses. You can also practice by solving coding challenges and working on personal projects to improve your skills.

5. What does the "if" statement do in Python?

The "if" statement in Python is used for conditional execution. It allows the program to make decisions based on certain conditions and perform different actions depending on the outcome of those conditions.

Similar threads

Replies
6
Views
1K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
28
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
982
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
2
Views
881
Back
Top