Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


what the difference between %r & %s


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 koka282   Members   -  Reputation: 235

Like
0Likes
Like

Posted 13 August 2014 - 07:14 PM

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


Sponsor:

#2 Dave Hunt   Crossbones+   -  Reputation: 2477

Like
0Likes
Like

Posted 13 August 2014 - 09:12 PM

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



#3 DiegoSLTS   Members   -  Reputation: 1869

Like
2Likes
Like

Posted 13 August 2014 - 09:13 PM

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



#4 koka282   Members   -  Reputation: 235

Like
0Likes
Like

Posted 14 August 2014 - 05:56 PM

still don't understand :(

sry for hard understanding :(



#5 DiegoSLTS   Members   -  Reputation: 1869

Like
1Likes
Like

Posted 14 August 2014 - 06:36 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS