What is the value of% r in python

what is the meaning of %r in the following statement?

 print '%r' % (1) 

I think I heard about %s , %d and %f , but I never heard about it.

+62
python sign
Mar 01 '10 at 7:13
source share
7 answers

Background:

Python has two built-in functions for turning an object into a string: str versus repr . str should be a friendly, human-readable string. repr should contain detailed information about the contents of the object (sometimes they return the same as for integers). By convention, if there is a Python expression that will be eval for another object that ==, repr will return such an expression, for example.

 >>> print repr ('hi')
 'hi' # notice the quotes here as opposed to ...
 >>> print str ('hi')
 hi

If returning an expression does not make sense for the object, repr should return a string surrounded by <and> characters, for example. <blah> .

To answer your original question:

%sstr
%rrepr

Besides:

You can control how you convert instances of your own classes to strings, __str__ and __repr__ .

 class Foo: def __init__(self, foo): self.foo = foo def __eq__(self, other): """Implements ==.""" return self.foo == other.foo def __repr__(self): # if you eval the return value of this function, # you'll get another Foo instance that == to self return "Foo(%r)" % self.foo 
+78
Mar 01 '10 at 8:48
source share
— -

It calls repr() object and inserts the resulting string.

+20
Mar 01 '10 at 7:15
source share

It prints the replacement as a string using repr() .

+4
Mar 01 '10 at 7:15
source share

Adding to the answers above, '%r' may be useful in a scenario where you have a list with a heterogeneous data type. Let, say, a list = [1, 'apple' , 2 , 'r','banana'] Obviously, in this case using '%d' or '%s' will result in an error. Instead, we can use '%r' to print all of these values.

+3
Mar 27 '15 at 9:33
source share

The difference between %r and %s is that %r calls the repr() method and %s calls the str() method. Both of these are Python built-in functions.

The repr() method returns a printable representation of this object. The str() method returns an "informal" or well-displayed representation of a given object.

Simply put, the str() method outputs the result the way the end user would like to see:

 name = "Adam" str(name) Out[1]: 'Adam' 

The repr() method prints or shows what the object actually looks like:

 name = "Adam" repr(name) Out[1]: "'Adam'" 
+2
Apr 21 '19 at 2:18
source share
 %s <=> str %r <=> repr 

%r calls repr() on the object and inserts the resulting string returned by __repr__ .

The string returned by __repr__ should be unambiguous and, if possible, match the source code needed to recreate the object being represented.

Quick example:

 class Foo: def __init__(self, foo): self.foo = foo def __repr__(self): return 'Foo(%r)' % self.foo def __str__(self): return self.foo test = Foo('Text') 

So,

 in[1]: test Out[1]: Foo('Text') in[2]: str(test) Out[2]: 'Text' 
+1
Dec 22 '17 at 19:11
source share

See String formatting operations in documents. Note that% s and% d, etc. May work differently, as you would expect if you are used to how they work in another language such as C.

In particular,% s also works well for int and float unless you have special formatting requirements where% d or% f will give you more control.

0
Mar 01 '10 at 8:26
source share



All Articles