Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 19th, 2012, 02:32 PM
whitewinter whitewinter is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 whitewinter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 47 sec
Reputation Power: 0
Help with format characters.

I am new to python and have a little question about format characters.

I did a few tests with them(I think the best way to learn is try it out) and I can't find the difference between %r and %s. It works with a floating point number, a number and strings. While all the others don't work with at least one of them. I already find out that %s converts the variable into a string with str() and %r with repr(). I also thought that %r is used for debugging.

A little example:

Code:
string = "This is just a string."
number = 3
floating_point = 3.0


# all the variables with %r

print "This is what I'm printing: %r" % string
print "This is what I'm printing: %r" % number
print "This is what I'm printing: %r" % floating_point


# all the variables with %s

print "This is what I'm printing: %s" % string
print "This is what I'm printing: %s" % number
print "This is what I'm printing: %s" % floating_point


# all the variables with %c(I know it will fail with 2 of them # 
# because it is used for things with 1 character).

print "This is what I'm printing: %c" % string
print "This is what I'm printing: %c" % number
print "This is what I'm printing: %c" % floating_point


Output:

Code:
This is what I'm printing: 'This is just a string.'
This is what I'm printing: 3
This is what I'm printing: 3.0
This is what I'm printing: This is just a string.
This is what I'm printing: 3
This is what I'm printing: 3.0
Traceback (most recent call last):
  File "3.py", line 23, in <module>
    print "This is what I'm printing: %c" % string
TypeError: %c requires int or char

Reply With Quote
  #2  
Old December 19th, 2012, 03:16 PM
Nyktos Nyktos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 76 Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 3 h 26 m 10 sec
Reputation Power: 2
Well...you've already found the difference then. %s uses str() and %r uses repr(). It just so happens that these are the same for most built-in types. If you define a class for which they are not the same...
Code:
class Foo:
    def __str__(self):
        return "blah blah"
    
    def __repr__(self):
        return "do dee do"

...you will see a difference:
Code:
>>> foo = Foo()
>>> "using s: %s" % foo
'using s: blah blah'
>>> "using r: %r" % foo
'using r: do dee do'


Normally __repr__ is meant to return something that could be entered into the interpreter to recreate the object while __str__ should return something "printable" (as str() is called by print()).

Reply With Quote
  #3  
Old December 20th, 2012, 04:31 AM
whitewinter whitewinter is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 whitewinter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 47 sec
Reputation Power: 0
Thank you!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Help with format characters.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap