what the difference between %r & %s

Started by
3 comments, last by DiegoSLTS 9 years, 8 months ago
hi all:)
i was learning from learnpythonthehardway.org
Exercise 14: Prompting and Passing
he write %r & %s !!!!
what the difference between %r & %s !!
VRx53jTB.png
Advertisement

A quick google search for python %r format yields this stack overflow post as the first result.

http://stackoverflow.com/questions/15170349/whats-the-difference-between-r-s-and-d-in-python

still don't understand :(

sry for hard understanding :(

Those %r and %s are placeholders for the actual text you'll see later when the code actually runs. You send an object to the print command for each %r, %s and any other "%" that should be replaced and python has to translate that object to text somehow so those placeholders can be replaced. When you use %r python will call the "repr" method with the object that corresponds to that position as a parameter, and will replace %r with the result or "repr()". When you use %s python will use a different method, "str".

Your classes should implement those methods in a way that makes sense, accodring to the documentation:

https://docs.python.org/2/library/functions.html#str

https://docs.python.org/2/library/functions.html#func-repr

This topic is closed to new replies.

Advertisement